一个同事不小心写了这样的代码:
A co-worker accidentally wrote code like this:
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对此进行了编译.
GCC 5.1.0 compiles this.
进行此编译的规则是什么?
What's the rule that makes this compile?
推荐答案注入了-此处使用类名,
出于查找(除非用于命名构造函数):这称为注入类名称
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 })是基于范围的for循环(因为C ++ 11),它会在 braised-init-list 由3个枚举数组成.
And then for (auto x : {foo::foobar, foo::fbar, foo::foob }) is a range-based for loop (since C++11), which iterates on the braced-init-list formed by the 3 enumerators.
更多推荐
为什么代码"foo :: foo :: foo :: foob"编译?
发布评论