gpt4 book ai didi

c++ - 在标准 (C++14) 的哪个地方说以下两个声明是等价的?

转载 作者:可可西里 更新时间:2023-11-01 17:40:15 24 4
gpt4 key购买 nike

struct A{};
int A;
struct A a;
struct A::A b;

上面的最后两个声明是等价的。它们都声明了类型 A 的对象。我可以在标准中的什么地方找到或推断出这一点?

最佳答案

[类]/2:

A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name.

A::A::A::A 也指 A。在某些情况下,A::A 可以改为命名构造函数,尽管 - [class.qual]/2 涵盖了这一点,它的注释甚至解决了您的示例:

In a lookup in which function names are not ignored33 and the nested-name-specifier nominates a class C

  • if the name specified after the nested-name-specifier, when looked up in C, is the injected-class-name of C (Clause 9), or
  • in a using-declaration (7.3.3) that is a member-declaration, if the name specified after the nested-name- specifier is the same as the identifier or the simple-template-id’s template-name in the last component of the nested-name-specifier,

the name is instead considered to name the constructor of class C. [ Note: For example, the constructor is not an acceptable lookup result in an elaborated-type-specifier so the constructor would not be used in place of the injected-class-name. — end note ]


33) Lookups in which function names are ignored include names appearing in a nested-name-specifier, an elaborated-type- specifier, or a base-specifier.

所以在这样的语句中

A::A a;

在查找 A::A 时,函数名称不会被忽略,因此代码格式错误,如 A::A指的是构造函数。然而,在

struct B : A::A {};
struct A::A a;

一切都很好,因为函数名称在基本说明符和详细类型说明符中被忽略。

关于c++ - 在标准 (C++14) 的哪个地方说以下两个声明是等价的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29705794/

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