为什么函数需要在使用之前声明? [英] Why do functions need to be declared before they are used?

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

问题描述

在阅读这个问题的一些答案时,我开始想知道为什么编译器实际上 需要知道一个函数,当它第一次遇到它。在解析收集所有在其中声明的所有符号的编译单元时,只是添加一个额外的通行不是很简单,以便它们被声明和使用的顺序不再重要了吗?

When reading through some answers to this question, I started wondering why the compiler actually does need to know about a function when it first encounters it. Wouldn't it be simple to just add an extra pass when parsing a compilation unit that collects all symbols declared within, so that the order in which they are declared and used does not matter anymore?

有人可能会说,声明函数之前使用肯定是好的风格,但我想知道,是否有任何其他原因为什么这是强制性的C ++?

One could argue, that declaring functions before they are used certainly is good style, but I am wondering, is there are any other reason why this is mandatory in C++?

编辑 - 要说明的示例:假设您必须在头文件中内联定义函数。这两个函数相互调用(也许是递归树遍历,其中树的奇数层和偶数层被不同地处理)。解决这个问题的唯一方法是在另一个函数前面声明一个函数。

Edit - An example to illustrate: Suppose you have to functions that are defined inline in a header file. These two function call each other (maybe a recursive tree traversal, where odd and even layers of the tree are handled differently). The only way to resolve this would be to make a forward declaration of one of the functions before the other.

一个更常见的例子(尽管有类而不是函数)使用 private 构造函数和工厂的类的情况。工厂需要知道类来创建它的实例,类需要知道 friend 声明的工厂。

A more common example (though with classes, not functions) is the case of classes with private constructors and factories. The factory needs to know the class in order to create instances of it, and the class needs to know the factory for the friend declaration.

如果这是要求是从旧的日子,为什么它在某个时候不被删除?

If this is requirement is from the olden days, why was it not removed at some point? It would not break existing code, would it?

推荐答案

您打算如何解决 undeclared 中定义的 标识符?

How do you propose to resolve undeclared identifiers that are defined in a different translation unit?

C ++没有模块概念,但是具有单独的翻译作为从C的继承.C ++编译器将自己编译每个翻译单元,不知道关于其他翻译单元所有。 (除了 export 打破了这一点,这可能是为什么,可悲的是,从来没有起飞。)

em> ,这里通常放置在其他翻译单元中定义的标识符的声明,实际上只是将同一声明转换为不同翻译单元的一种非常笨拙的方式。它们不会使编译器知道存在其中定义了标识符的其他翻译单元。

C++ has no module concept, but has separate translation as an inheritance from C. A C++ compiler will compile each translation unit by itself, not knowing anything about other translation units at all. (Except that export broke this, which is probably why it, sadly, never took off.)
Header files, which is where you usually put declarations of identifiers which are defined in other translation units, actually are just a very clumsy way of slipping the same declarations into different translation units. They will not make the compiler aware of there being other translation units with identifiers being defined in them.

编辑 了解您的其他示例:

所有文字包含正确的模块概念,编译已经花费了很长时间为C ++,所以需要另一个编译通过(编译已经被分成几个遍,而不是所有的可以优化和合并,IIRC)会恶化已经很糟糕的问题。并且改变这可能会改变一些情况下的重载分辨率,从而打破现有代码。

Edit re your additional examples:
With all the textual inclusion instead of a proper module concept, compilation already takes agonizingly long for C++, so requiring another compilation pass (where compilation already is split into several passes, not all of which can be optimized and merged, IIRC) would worsen an already bad problem. And changing this would probably alter overload resolution in some scenarios and thus break existing code.

请注意,C ++确实需要额外的通行来解析类定义,因为在类定义中内联定义的成员函数被解析,就像它们在类定义。然而,这是决定时C与类的思想,所以没有现有的代码库打破。

Note that C++ does require an additional pass for parsing class definitions, since member functions defined inline in the class definition are parsed as if they were defined right behind the class definition. However, this was decided when C with Classes was thought up, so there was no existing code base to break.

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

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