anonymous empty untagged classes, structs, unions, and enums
!Disclaimer!: Please note that I am talking about untagged declarations. I know it's a possibly informal term and what I mean by it is that the declaration is not a typedef and it does not declare an object of the anonymous type. In these cases everything is clear.
Question 1:
which of the following is a well-formed C++ program? (multiple-choice)//One
int main(){
struct{}; //or class{}; should be the same
}
//Two
int main(){
enum{};
}
//Three
int main(){
union{};
}
Remarks:
MSVC9.0 accepts all three. On number one it gives a warning. On number two and three we get 0 errors and 0 warnings. The online comeau accepts only number three, numbers one and two fail to compile with a开发者_C百科 diagnostic message "declaration does not declare anything". If, in number two, the anonymous enumeration contains at least one enumerator, number two is rightfully accepted by the comeau compiler.If the comeau compiler is correct, and this is the standard behavior, then I see some inconsistency in it. I can understand why number one should be rejected. I would also understand why number two should be rejected(declares nothing), but in this case number three should be rejected as well. And if number three should not be rejected, then the only difference I see with number one is that the scope of the members of an anoymous union is the outer scope. But that is also the case with enums, so, my claim is that should number three be accepted then so should number two.
Question 2:
If comeau is correct, what considerations am I missing in my above judgment?P.S. I have written an email to comeau support. I will post their answer as soon as I get it.
None of it is legal. See [dcl.dcl] paragraph 3.
精彩评论