Search code examples
c++classsyntaxlanguage-lawyergrammar

Is it portable to have a stray semicolon in a class' declaration


class Test
{
  ;
  int x;
};

Is this perfectly legal and portable?


Solution

  • From my reading of the standard, this is not allowed.

    If you look at the grammar definition only, it seems to allow it. The relevant parts are:

    The member-specification is what appears between the { ... } in the class declaration.

    member-specification is a sequence of member-declaration and access specifiers. One possible form for a member-declaration is:

    attribute-specifier-seqopt decl-specifier-seqopt member-declarator-listopt ;

    Since everything before the semicolon is optional, it looks like it's allowed to have an empty member-declaration, which consists of only a semicolon.

    However, 9.2/1 says:

    Except when used to declare friends (11.3) or to introduce the name of a member of a base class into a derived class (7.3.3), member-declarations declare members of the class, and each such member-declaration shall declare at least one member name of the class.

    Since an empty member-declaration does not declare at least one member of a class, it seems that this is not standard-compliant, even if some compilers accept it.