c++ - 为什么编译代码 "foo::foo::foo::foob"?

标签 c++ c++11 gcc language-lawyer names

一位同事不小心写了这样的代码:

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::foobfoo::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/

相关文章:

c++ - 无法使用嵌套的 lambda 捕获静态成员

c++ - 关于C++0x中右值的问题

c++ - 如何将大量秒数可移植地添加到 time_t 对象?

c++ - 开发-C++/TDM-GCC : Linkage Problems with Boost Libaries Downloaded from boost. 组织

c++ - 为什么 C++ 模板参数应该声明为类类型?

c++ - 我在哪里可以找到 MFC 中的 list ?

c++ - 如何让 char[] 与 std::map 一起工作

c++ - 枚举唯一的无向路径的有效方法

node.js - 为 Node gyp 添加包含目录

c++ - boost::variant 访问者返回错误(最烦人的解析?)