为什么代码 "foo::foo::foo::foob" 正在编译?

Why is the code "foo::foo::foo::foob" compiling?

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

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 })是由3个枚举数组成的range-based for loop (since C++11), which iterates on the braced-init-list

我将此代码更改为:

#include <initializer_list>
#include <iostream>
struct foo {
  foo() : baz(foobar) {}
  enum bar {foobar, fbar, foob};
  bar baz;
};

int main() {
  for( auto x : { foo::foobar,
                  foo::fbar,
                  foo::
                  foo::
                  foo::foob } )
                  {
                      std::cout << "x=" << x << std::endl;
                  }
  return 0;
}

for 循环运行了 3 次。输出是:"x=1 x=2 x=3".


foo::foo::foo::foob 等同于foo::foob。 所以

for( auto x : { foo::foobar,
                  foo::fbar,
                  foo::
                  foo::
                  foo::foob } )

一样

for( auto x : { foo::foobar, foo::fbar, foo::foob } )
{
}

表示x{ foo::foobar, foo::fbar, foo::foob }

范围内