gpt4 book ai didi

c++ - 标准中的哪个地方说成员别名声明可以像静态成员一样使用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:44:08 24 4
gpt4 key购买 nike

考虑以下片段:

#include <iostream>
struct A { int i; using Int = int; };

int main()
{
std::cout << sizeof(A::Int) << '\n';
}

clang中编译执行正常和海湾合作委员会。我知道这看起来很明显,但我在标准 (C++14) 中找不到任何支持在 main() 中引用 A::Int 的内容。

最佳答案

这只是您的正常限定查找。来自 [basic.lookup.qual]:

The name of a class or namespace member or enumerator can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-specifier that denotes its class, namespace, or enumeration.

然后来自 [class.qual]:

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-namespecifier is looked up in the scope of the class (10.2), except for the cases listed below. The name shall represent one or more members of that class or of one of its base classes (Clause 10). [ Note: A class member can be referred to using a qualified-id at any point in its potential scope (3.3.7). —end note ] The exceptions to the name lookup rule above are the following:

  • a destructor name [...]
  • a conversion-type-id of a conversion-function-id [...]
  • the names in a template-argument of a template-id [...]
  • the lookup for a name specified in a using-declaration [...]

您示例中的nested-name-specifierA,它是一个类。这些异常(exception)均不适用。所以我们只在类的范围内查找名称 Int

来自 [dcl.typedef]:

A name declared with the typedef specifier becomes a typedef-name. Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type.
[...]
A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier.

所以你的alias-declaration将名称Int引入到A的范围内,这是根据我刚才的限定查找规则找到的枚举。

关于c++ - 标准中的哪个地方说成员别名声明可以像静态成员一样使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30552141/

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