一位同事不小心写了这样的代码:
struct foo {
foo() : baz(foobar) {}
enum bar {foobar, fbar, foob};
bar baz;
};
void f() {
for( auto x : { foo::foobar,
foo::fbar,
foo::
foo::
foo::foob } );
// ...
}
GCC 5.1.0 编译这个。
编译的规则是什么?
最佳答案
injected-class-name这里用到了,
the name of the class within its own definition acts as a public member type alias of itself for the purpose of lookup (except when used to name a constructor): this is known as injected-class-name
然后
foo::
foo::
foo::foob
即foo::foo::foo::foob
与 foo::foob
相同。
然后 for (auto x : {foo::foobar, foo::fbar, foo::foob })
是 range-based for loop (since C++11) , 迭代 braced-init-list由 3 个枚举数组成。
关于c++ - 为什么编译代码 "foo::foo::foo::foob"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46805449/