Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolons in a class definition

Tags:

c++

syntax

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];
  };
};

The 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?
     };
    
like image 405
Armen Tsirunyan Avatar asked Mar 19 '11 23:03

Armen Tsirunyan


People also ask

What is the use semi colon in the class *?

Use a semicolon to connect related independent clauses. To connect two independent clauses that express related ideas, place the semicolon between the first independent clause and the second independent clause—no conjunction necessary. (Conjunctions are connective words like "and," "but," and "or.”)

What is semicolons and examples?

When to Use a Semicolon. A semicolon (;) is a punctuation mark that has two main functions: Semicolons separate items in a complex list. For example, The Council is comprised of ten members: three from Sydney, Australia; four from Auckland, New Zealand; two from Suva, Fiji; and one from Honiara, Solomon Islands.

Whats is a semicolon?

A semicolon is most commonly used to link (in a single sentence) two independent clauses that are closely related in thought. When a semicolon is used to join two or more ideas (parts) in a sentence, those ideas are then given equal position or rank.


2 Answers

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.

like image 189
Johannes Schaub - litb Avatar answered Oct 20 '22 06:10

Johannes Schaub - litb


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

like image 30
Maciej Ziarko Avatar answered Oct 20 '22 05:10

Maciej Ziarko