开发者

Semicolons in a class definition

开发者 https://www.devze.com 2023-02-18 23:36 出处:网络
I was reading this gotw and here\'s a code sample from there: struct X { static bool f( int* p ) { return p && 0[p] and not p[1:>>p[2];

I was reading this gotw and here's a code sample from there:

struct X {
  static bool f( int* p )
  {
    return p && 0[p] and not p[1:>>p[2];
  };
};

开发者_如何学JAVAThe question was: how many errors should a compliant compiler give:

I answered one, because this code is equivalent to

struct X {
  static bool f( int* p )
  {
    return p && p[0] && ! p[1] > p[2];
  };
};

And I was thinking that a semicolon after a static function definition would be an error. But Mr. Sutter says 0 and explains (apart from the things I do understand) that

The "extra" semicolon is allowed at the end of a function declaration.

My question is:

  • What text in the standard allows this?
  • Does this refer to member functions only?
  • Can a semicolon appear between two members or in any other place in a class definition as in

     struct X
     {
       int a;;;;int b; //Legal?
     };
    


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消