例:
int main() { int a = 0; struct X { decltype(a) a; }; return 0; }
decltype(a)是指主要的本地a,而它声明的成员共享相同的名称.
Clang编译没有任何问题,MSVC14也是如此.
prog.cc:6:21: error: declaration of 'int main()::X::a' [-fpermissive] decltype(a) a; ^ prog.cc:3:9: error: changes meaning of 'a' from 'int a' [-fpermissive] int a = 0;
哪种行为符合标准?
解决方法
我相信这违反了[basic.scope.class] / 1(N3337):
The following rules describe the scope of names declared in classes.
1) […]
2) A name
N
used in a classS
shall refer to the same declaration in its context and when re-evaluated in the completed scope ofS
. No diagnostic is required for a violation of this rule.
由于decltype(a)是指在声明成员变量之前的封闭范围内的声明,而是在“X的完整范围内重新评估”时引用该成员,该程序是不正确的.不需要诊断,但GCC提供一个(尽管它是相当神秘的).所有三个编译器的行为都是有效的.