为什么C ++需要前向声明? [英] Why does C++ require forward declarations?

查看:96
本文介绍了为什么C ++需要前向声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这同样适用于函数原型。为什么我们要两次告诉

编译器?其他现代编译语言没有这个

的要求。


只是好奇,


Aleko

This applies equally to function prototypes. Why do we have to tell the
compiler twice? Other modern compiled languages don''t have this
requirement.

Just curious,

Aleko

推荐答案

aleko写道:
这同样适用于函数原型。为什么我们要两次告诉编译器?其他现代编译语言没有这个
要求。
This applies equally to function prototypes. Why do we have to tell
the compiler twice? Other modern compiled languages don''t have this
requirement.




你是什么意思告诉编译器两次?当你编译

一个模块,其中的函数只被调用而没有定义,

是什么'是'两次'"关于它?


另外,考虑到C ++有转换。如果你的函数,比如,

说,myspecialmathfunction,只需要''double''参数,而你

提供一个'int'类型的值,怎么会编译器知道它在调用该函数时必须将''int''转换为''double'',除非它有一个原型(声明)来引导它?如果没有应用

转换,则可能形成错误的堆栈帧,并且行为将不正确。


还有哪些其他语言没有这个要求?在Pascal

中,您必须在使用*之前定义所有使用的函数。在

C中你需要声明它们。在Fortran ......我现在还不知道关于Fortran的价值是多少?自从我上次使用它以来,它在几十年来发生了巨大的变化。当然有几百种其他语言,你可能没有必要在使用它之前声明

函数,你可能知道从那以后你是这么说的。


V



What do you mean by "tell the compiler twice"? When you compile
a module in which the functions are only called and not defined,
what''s "twice" about it?

Also, consider that C++ has conversions. If your function, like,
say, myspecialmathfunction, only takes ''double'' argument, and you
provide a value of type ''int'', how would the compiler know that it
has to convert your ''int'' to ''double'' when calling that function
unless it has a prototype (declaration) to guide it? And if the
conversion is not applied, a wrong stack frame can be formed and
the behaviour will be incorrect.

And what other languages do not have that requirement? In Pascal
you have to define all functions used *before* they are used. In
C you need to declare them as well. In Fortran... I don''t know
about Fortran nowadays, it changed significantly in the decades
passed since I used it last time. Of course there are hundreds of
other languages an in some you probably don''t have to declare the
function before it''s used, and you probably know since you said so.

V




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

新闻:9N ******************** @ comcast.com ...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:9N********************@comcast.com...
和还有哪些其他语言没有这个要求?在Pascal
中,您必须在使用*之前定义所有使用的函数。在
C中你也需要声明它们。在Fortran ......我现在不知道关于Fortran的事情,自从我上次使用它以来几十年来它发生了重大变化。当然,有几百种其他语言,你可能没有必要在它使用之前声明
函数,你可能知道,因为你这么说。
And what other languages do not have that requirement? In Pascal
you have to define all functions used *before* they are used. In
C you need to declare them as well. In Fortran... I don''t know
about Fortran nowadays, it changed significantly in the decades
passed since I used it last time. Of course there are hundreds of
other languages an in some you probably don''t have to declare the
function before it''s used, and you probably know since you said so.




Java和C#都没有这个要求。再说一次,当C ++被构建时,编写一个两遍编译器就更难了,Java和C#是更简单语言的简单语言。这只是编译器的问题

在尝试链接之前知道所有可用的功能。我的意思是,

信息是在某个地方声明的,如果编译器只有早期在翻译单元中寻找声明,那就更容易了。
/>

- JFA1



Neither Java nor C# have that requirement. Then again, when C++ was being
built, it was harder to write a two-pass compiler, and Java and C# are
simpler languages with simpler grammars. It''s just a matter of the compiler
knowing all of the functions available before it tries to link. I mean, the
information is declared somewhere, it''s just easier if the compiler only has
to look earlier in a translation unit to find a declaration.

- JFA1


它允许编译器成为一次通过。编译器。 Java和C#

大概需要读取每个输入文件两次:一次获取

函数和方法名称,一次执行实际编译。


请注意,编译器需要知道函数'

参数的确切类型,这些参数将被调用以便键入check并生成
$ b当他们被调用时,$ b $适当的转换(例如,int到双重促销)。换句话说,编译器需要知道呼叫站点上该函数的

签名,因此它需要具有

a前向声明或者已经读完其余的文件

已经。


(这不是严格正确的;它可能会生成一个

通行证编译器将工作,但它需要存储所有呼叫站点的位置

,以便它可以返回并插入正确的代码。

但这将是很难。)


我不知道编译器实现难度/速度是否导致了这个要求的推理,但它至少是一个

实质性后果。

It allows the compiler to be a "one pass" compiler. Java and C#
presumably need to read through each input file twice: once to get the
function and method names and once to do the actual compilation.

Note that the compiler needs to know the exact type of the function''s
arguments that will be called in order to type check and generate the
appropriate conversions (for instance, int to double promotions) when
they are called. In other words, the compiler needs to know the
signature of the function at the call sites, so it either needs to have
a forward declaration or have read through the rest of the file
already.

(This is not strictly true; it''s probably possible to generate a one
pass compiler that will work, but it would require storing the location
of all call sites so that it could go back and insert the proper code.
But this''d be difficult.)

I don''t know if compiler implementation difficulty/speed is the
reasoning that led to this requirement, but it is at least a
substantial consequence.


这篇关于为什么C ++需要前向声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆