为什么C ++不允许在声明之前使用函数? [英] Why did C++ never allow functions to be used before they're declared?

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

问题描述

好的,我知道这看起来像是一个重复的为什么函数需要在使用之前声明?,但是似乎现有的答案不能完全解决所有的细节问题。

OK, I know this looks like a duplicate of Why do functions need to be declared before they are used? but it doesn't seem like existing answers fully address all the details.

我知道C ++最初是在80年代设计的,所以它可以在一个单一的传递,因为计算机是缓慢的翻译。好。但最近的标准是在2011年发布的,所以我不知道为什么C ++编译器不能做现在需要多遍。它仍然会伤害性能,是的,但只有当它实际上成为必要。因此,以下内容仍然只需要一次传递:

I know that C++ was originally designed in the 80's so it could be translated in a single pass, because computers were slow. OK. But the most recent standard was published in 2011, so I don't see why C++ compilers can't do things now that require multiple passes. It would still hurt performance, yes, but only if it actually became necessary. So the following would still only require a single pass:

void foo();
int main() { foo(); }
void foo() {}

(和更慢),因为它不知道 foo 是一个函数或类型,直到它看到以下声明:

whereas for the following, the compiler could make two (and be slower), because it doesn't know whether foo is a function or a type until it sees the declaration below:

int main() { foo(); }
void foo() {}

如果您试图使用函数而不声明它首先,并且声明根本不在当前翻译单元中, 然后 它将是一个错误。但是如果它在同一个翻译单元,那么编译器可以只做额外的传递。

and if you tried to use a function without declaring it first, and the declaration is not in the current translation unit at all, then it would be an error. But if it's in the same translation unit then the compiler could just make additional passes.

我的同事认为,这样的功能将节省大量的开发人员时间,声明和定义未匹配的问题。我相信这已经提出了很多次,并且每次都被拒绝。

My colleague argues that such a feature would save a lot of developer time, and would avoid issues with the declaration and definition not being matched. And I'm sure this has been proposed many times over and rejected every time. What is the actual reasoning behind rejecting it, i.e., the committee's rationale?

推荐答案

是什么? >

模板解析



请考虑以下代码:

Template parsing

Consider the following line of code:

a < b , c > d;

如何解析这个?实际上有两种方法,取决于 a b c d 是。首先,变量声明

How would you parse this? There are actually two ways, depending on what a, b, c and d are. Firstly, a variable declaration

a<b,c>   d;
^^^^^^   ^
 Type   Var

a 是已知的模板类型, b c 其他已知类型。其次,

in the case that a is a known template type, b and c are other known types. Secondly,

   a<b   ,   c<d ;
   ^^^       ^^^ 
boolean expressions

code> a , b c d 是某种类型的变量。

in the case that a, b, c and d are all variables of some sort.

或另一个:

a b(c); // is 'b' a function or a variable?

这可以是一个函数声明(返回类型 a 和参数类型 c )或变量定义(其类型为 a c )。

This could be a function declaration (a function with return type a and argument type c) or a variable definition (whose type is a and whose constructor argument is c).

的东西,不幸的是。我不知道,如果写一个编译器可以处理这种东西,但是它至少是很难写一个。编译时间是C ++中的一个严重问题。这只会使情况更糟。

There's a lot of stuff like that, unfortunately. I'm not sure, if it would be impossible to write a compiler that can deal with that kind of stuff, but it would at least be very hard to write one. Compilation times are a serious issue in C++ already. This would only make it worse. Also: It is good practice to only use what you have defined or declared already, even in other languages.

即使合理可能实现此功能,也会杀死向后兼容性。函数重载分辨率仅考虑先前的声明,并且函数调用的解释可能根据写入函数调用的位置而改变。这是C ++放在一起的方式。现在,C ++标准委员会对后台兼容性很重要。特别是:他们不想破坏任何现有的代码(正确的)。你的建议肯定会打破现有的代码,这对语言设计师来说是一个没有用的。

Even if it would be reasonably possible to implement this feature, it would kill backwards compatibility. Function overloading resolution only takes prior declarations into account and the interpretation of function calls may change depending on the place a function call is written. That's the way C++ is put together. Now, the C++ standards committee is big on back-wards compatibility. In particular: They do not want to break any existing code (and rightly so). Your proposal would surely break existing code which is a no-go for the language designers.

这篇关于为什么C ++不允许在声明之前使用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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