Motivated by the discussion
The grammar for C++ classes is defined as
class-key identifier *[opt]* base-clause *[opt]* (Italics are mine)
This to me means that the class name is option and we can have unnamed classes in C++.
So, is the following well-formed?
struct X{
struct{
int x;
int y;
};
};
int main(){}
VS: error C2467: illegal declaration
of anonymous ‘struct’Comeau online: error: declaration does
not declare anything
struct{GCC(ideone): Compiles fine
Any thoughts?
No, it is not well-formed. You cannot derive the language syntax from these grammar statements alone. The extra requirements are given in the text of the standard also have to be taken into account. In this case that would be
The last sentence is the one that matters in this case
The "optional" part is only there to allow declarations like
The first one which declares a class type with no linkage and variable
sof that type. Note that types with no linkage cannot be used to declare a variable with linkage, meaning that such declaration cannot be used in namespace scope.Your example is ill-formed, but this would be legal
Also, nameless class syntax is used to declare anonymous unions (although I’m a bit puzzled by the fact that 7/3 does not mention anonymous unions).