Semicolons in a class definition

Yes, a semicolon is explicitly allowed after a function declaration in a class specifier. As a result, currently in the C++0x draft, the following is valid too: The first semicolon belongs to the function definition, the second to the class specifier delegating to the function-definition non-terminal.

struct A {
  void f() = delete;;
};

But three semicolons would be illegal. As are two semicolons after a function definition having a body. The respective text in the spec is the grammar at 9.2[class.mem].

Semicolons after function definitons were allowed already in C++03, but they were not allowed at namespace scope after function definitions. C++0x fixes that by introducing empty-declarations. But those only appear when you have a semicolon after function definitions outside class bodies.

Sutter talks about "extra" semicolons at the end of function declarations though, which is not entirely correct. Because the following is invalid syntax

struct A {
  void f();; // invalid!
};

An extra semicolon in a class specifier is only valid after a function-definition. Also, as a checkup at 9.2 uncovers, it's not valid when the function definition is a template

struct A {
  template<typename T> void f() { }; // invalid!
};

This is because it is parsed by a template-declaration (which will itself parse the remaining text to function-definition eventually) for which the class specifier does not have an additional ; afterwards.


; is an empty statement. You can have as many empty statements as you want. It is absolutely correct.

int foobar(int arg)
{
    return 0;
};

is the same as

int foobar(int arg)
{
    return 0;
}
/*DO NOTHING*/;

Similar question: Why are empty expressions legal in C/C++?

Edit:

A declaration of a member is also a statement. So empty statement inside class is something like an << empty >> declaration statement inside class :)

Tags:

C++

Syntax