Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why class { int i; }; is not fully standard-conformant?

This is a follow-up question.

In the previous question, @JohannesSchaub-litb said that the following code is not fully standard-conformant:

class { int i; };  //unnamed-class definition. § 9/1 allows this! 

and then he added,

while it is grammatically valid, it breaks the rule that such a class must declare at least one name into its enclosing scope.

I couldn't really understand this. What name is he talking about?

Could anyone elaborate on this further (preferably quoting the Standard)?

like image 252
Nawaz Avatar asked Oct 30 '12 12:10

Nawaz


1 Answers

Clause 9 of the standard allows class {public: int i;} (note the lack of a final semicolon) because this decl-specifier-seq for an unnamed class might be used in some other construct such as a typedef or a variable declaration. The problem with class {public: int i;}; (note that the final semicolon is now present) is that this class specification now becomes a declaration. This is an illegal declaration per clause 7, paragraph 3 of the standard:

In such cases, and except for the declaration of an unnamed bit-field (9.6), the decl-specifier-seq shall introduce one or more names into the program, or shall redeclare a name introduced by a previous declaration.

like image 173
David Hammen Avatar answered Sep 19 '22 17:09

David Hammen