Reputation: 143
I was amazed when I saw that this code success compiled with MS Visual C++.
struct foo {
struct foo(int i): value(i) {}
int value;
};
What means keyword struct
in such strange context?
Upvotes: 9
Views: 165
Reputation: 254441
In most contexts, you can use the elaborated type specifier struct foo
, or equivalently class foo
, instead of just the class name foo
. This can be useful to resolve ambiguities:
struct foo {}; // Declares a type
foo foo; // Declares a variable with the same name
foo bar; // Error: "foo" refers to the variable
struct foo bar; // OK: "foo" explicitly refers to the class type
However, you can't use this form when declaring a constructor, so your compiler is wrong to accept that code. The specification for a constructor declaration (in C++11 12.1/1) only allows the class name itself, not an elaborated type specifier.
In general, you shouldn't be surprised when Visual C++ compiles all kinds of wonky code. It's notorious for its non-standard extensions to the language.
Upvotes: 10