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

前端 未结 5 2119
深忆病人
深忆病人 2020-12-14 00:39

This is a follow-up question.

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

5条回答
  •  余生分开走
    2020-12-14 00:49

    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.

提交回复
热议问题