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 compiles this.
What's the rule that makes this compile?
I changed this code to this:
for loop runs 3 times. output is: "x=1 x=2 x=3".
foo::foo::foo::foob
is the samefoo::foob
. Sois the same
It means that
x
is in range{ foo::foobar, foo::fbar, foo::foob }
The injected-class-name is used here,
then
i.e.
foo::foo::foo::foob
is same asfoo::foob
.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.