[英]Where in the Standard does it say that the default member initializer for U::j should be ignored by the compiler?
[英]Where in the Standard does it say that a member alias-declaration can be used as if it was a static member?
请考虑以下代码段:
#include <iostream>
struct A { int i; using Int = int; };
int main()
{
std::cout << sizeof(A::Int) << '\n';
}
它在clang和GCC中正常编译和执行。 我知道这看起来很明显,但我在标准(C ++ 14)中找不到支持main()
对A::Int
的引用的A::Int
。
这只是您正常的合格查找。 来自[basic.lookup.qual]:
在将
::
scope resolution运算符(5.1)应用于表示其类,名称空间或枚举的嵌套名称说明符之后,::
类或名称空间成员或枚举数的名称 。
然后从[class.qual]:
如果qualified-id的nested-name-specifier指定一个类,则在类(10.2)的范围内查找在嵌套的namespecifier之后指定的名称,但下面列出的情况除外。 该名称应代表该类别或其基类之一的一个或多个成员(第10条)。 [注意:可以在潜在范围内的任何一点使用qualified-id引用类成员(3.3.7)。 -end note]以上名称查找规则的例外情况如下:
- 析构函数名称[...]
- 转换 函数 id的转换类型 ID [...]
- 模板ID的模板参数中的名称[...]
- 查找using声明中指定的名称[...]
示例中的嵌套名称说明符是A
,它是一个类。 这些例外都不适用。 所以我们只是在类的范围内查找名称Int
。
来自[dcl.typedef]:
使用
typedef
说明符声明的名称将成为typedef-name 。 在其声明的范围内, typedef-name在语法上等同于关键字,并按照第8章中描述的方式命名与标识符关联的类型。因此, typedef-name是另一种类型的同义词。
[...]
也可以通过别名声明引入typedef-name 。 using关键字后面的标识符变为typedef-name,并且该标识符后面的可选attribute-specifier-seq属于该typedef-name 。 它具有与typedef
说明符引入的语义相同的语义。
因此,您的别名声明将名称Int
引入A
的范围,该范围根据我刚刚枚举的限定查找规则找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.