Why Must I Put a Semicolon at the End of Class Declaration in C++

Why must I put a semicolon at the end of class declaration in C++?

The full syntax is, essentially,

class NAME { constituents } instances ;

where "constituents" is the sequence of class elements and methods, and "instances" is a comma-separated list of instances of the class (i.e., objects).

Example:

class FOO {
int bar;
int baz;
} waldo;

declares both the class FOO and an object waldo.

The instance sequence may be empty, in which case you would have just

class FOO {
int bar;
int baz;
};

You have to put the semicolon there so the compiler will know whether you declared any instances or not.

This is a C compatibility thing.

Why mustn't I put a semicolon at the end of class declaration in C#

Because that is what the language specification says.

Remember that C# wasn't designed with any C++ compatibility in mind. The language designers has simply decided, that the ending semi-colon is not needed.

Semicolon after class declaration braces

The semi-colon after the closing brace in a type declaration is required by the language. It's been that way since the earliest versions of C.

And yes, people do indeed do the declaration you just put up there. It's useful for creating scoped types inside of methods.

void Example() {
struct { int x; } s1;
s1.x = 42;

struct ADifferentType { int x; };
}

In this case, I think it's clear why the semi-colons are needed. As to why it's needed in the more general case of declaring in the header file I'm unsure. My guess is that it's historical and was done to make writing the compiler easier.

When is a semicolon after } mandated in c/c++?

int a[2] = {1,2}, j = 5;

When initialization of array or structure are done with {} all the subsequent variables are declared after ,.

Edit: As you changed your question; ; is mandatory after the class, enum, initialization syntax declarations.

class A {};  // same for `struct
enum E {}; // enum class (c++0x)
int a[] = {1,2}; // array or object initialization

And as per comment by @rubenvb:

do {} while(condition);

Why is the semicolon not required but allowed at the end of a class definition?

I've compiled and executed both the program snippets shown for Java, and they both work. Can anyone explain why this is so?

It is allowed because the Java Grammar says it is allowed; See JLS 7.6.

What does the semicolon at the end of a class definition in Java stand for?

Nothing. It is optional "syntactic noise".

The JLS explains it as follows:

Extra ";" tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers who are used to placing ";" after a class declaration. They should not be used in new Java code.


(Note that this is NOT an "empty statement". An empty statement (JLS 14.6) appears in a syntactic context where a statement is allowed. The presence of an empty statement can change the meaning of your code; e.g. if (a == b) ; c(); versus if (a == b) c();)

C++ Semicolon after class, struct, enum,... useless but compilers still emit errors?

It's a leftover from C. You can declare variables after cladd/struct/enum declaration. the code:

class A{};
A a;

Is identical to:

class A{} a;

The compiler needs the ; to know you don't want to declare variables after the class declaration.

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.

Semicolon after Function

Not really, the semicolon there makes no difference. It's probably a matter of habit.

You can put as many semicolons if you want though in C++11:

void foo() {

};;;;;;;;


Related Topics



Leave a reply



Submit