gpt4 book ai didi

c++ - 在类中的定义之前解析声明

转载 作者:行者123 更新时间:2023-11-28 03:35:48 24 4
gpt4 key购买 nike

这个问题here稍微勾起了我的兴趣。 C++ 标准中是否有任何地方指定类中的所有声明必须 在成员函数的任何伴随实现之前被解析?我见过其他一些与此类似的问题,但在任何答案中都没有提到该标准。

最佳答案

标准没有指定编译器应该如何解析翻译单元。相反,它指定在任何地方使用任何标识符来引用声明都是无效的。

3.3.2p5:

After the point of declaration of a class member, the member name can be looked up in the scope of its class. [ Note: this is true even if the class is an incomplete class. ]

3.3.7p1:

The following rules describe the scope of names declared in classes.

  1. The potential scope of a name declared in a class consists not only of the declarative region following the name’s point of declaration, but also of all function bodies, brace-or-equal-initializers of non-static data members, and default arguments in that class (including such things in nested classes).
  2. A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.
  3. If reordering member declarations in a class yields an alternate valid program under (1) and (2), the program is ill-formed, no diagnostic is required.
  4. A name declared within a member function hides a declaration of the same name whose scope extends to or past the end of the member function’s class.
  5. The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, member function definitions (including the member function body and any portion of the declarator part of such definitions which follows the declarator-id, including a parameter-declaration-clause and any default arguments (8.3.6).

关于c++ - 在类中的定义之前解析声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com