views:

1923

answers:

7

Apologies in advance for what is probably a stupid question, but in C++ classes, why the semi-colon after the closing brace? I regularly forget it and get compiler errors, and hence lost time. Seems somewhat superfluous to me, which is unlikely to be the case. Do people really do things like

class MyClass
{
.
.
.
} MyInstance;

Edit: I get it from a C compatibility point of view for structs and enums, but since classes aren't part of the C language I guess it's primarily there the keep consistency between similar declaration constructs. What I was looking for was more related to design rationale rather than being able to change anything, although a good code completion IDE might trap this before compilation.

+6  A: 

It's a language design issue. I think the compiler would do without it (as C# compiler does), so it's not technical at all. They chose it to be like that.

Clarification: I don't know the reason for downvotes. The only reason I can think of is the misconception that C++ does not require it. It does, indeed. What I am saying is a compiler doesn't essentially need it to be able to work. Specifically, C++ language designers chose to require it.

Mehrdad Afshari
Downvoters: I really appreciate to know if you can tell me why you can't build a compiler that does without the semicolon...
Mehrdad Afshari
Voted you back up. Not an unreasonable answer - you might have pointed out also that even in C++, namespaces don't need a semicolon on their closing brace.
James Hopkin
It is a variable declaration, and it needs a semicolon. In C++ as well as in C#. Of course this is a language design decision, the fact that variable declarations need a semicolon. Class declarations don't need it, they have curly braces. Variable declarations need it. It's not an arbitrary design decision as you say. It's important and consistent.
Stefan Steinegger
@Stefan Steinegger: I didn't say it's "arbitrary". I meant it's not necessary from the compilation perspective. BTW, they could have not required it if it's not followed by a variable (ensuring consistency with block structures such as namespaces and methods). Anyway, what I wanted to point out is "they could have built a compiler that does without it, it's not necessary for distinguishing stuff"
Mehrdad Afshari
Ok, got it. My fault, took the down-vote back.
Stefan Steinegger
+10  A: 

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.

JaredPar
Why can't I just create scoped type without specifying MyInstance? It seams strange as you combine two actions: declaring new type and declaring new variable.
Mykola Golubyev
@Mykola you can do both. See the sample I added
JaredPar
+6  A: 

I guess it's because classes are declarations, even when they need braces for grouping. And yes, there's the historical argument that since in C you could do

struct
{
  float x;
  float y;
} point;

you should in C++ be able to do a similar thing, it makes sense for the class declaration to behave in the same way.

unwind
+3  A: 

It's short for

class MyClass
{
.
.
.
};

// instance declaration
MyClass MyInstance;  // semicolon here

The semicolon after the curly braces of the class declaration is actually overkill, but it is how C++ is defined. The semicolon after the variable declaration is always needed and makes sense.

Stefan Steinegger
Did you mean to *not* put a semicolon after the class declaration? I doubt this will compile!
xtofl
Thanks, was a mistake, fixed it. My C++ gets rusty. :-)
Stefan Steinegger
So, does C++ require a semi-colon after each declaration ?
Loai Najati
+1  A: 

I do not use such declarations

class MyClass
{
.
.
.
} MyInstance;

But in this case I can understand why is semicolon there.
Because it is like int a; - variable declaration.

Probably for consistence as you can omit 'MyInstance' semicolon stays there.

Mykola Golubyev
A: 

It is needed after a struct for compatibility reasons, and how would you like this:

struct MyStruct { ... };
class  MyClass  { ... }    //inconsistency
Zifre
A: 

In C/C++ the ; is a statement terminator. All statements are terminated with ; to avoid ambiguity (and to simplify parsing). The grammar is consistent in this respect. Even though a class declaration (or any block for that matter) is multiple lines long and is delimited with {} it is still simply a statement (the { } is part of the statement) hence needs to be terminated with ; (The ; is not a separator/delimitor)

In your example

class MyClass{...} MyInstance;

is the complete statement. One could define multiple instances of the declared class in a single statement

class MyClass{...} MyInstance1, MyInstance2;

This is completely consistent with declaring multiple instances of a primitive type in a single statement:

int a, b, c;

The reason one does not often see such desclaration of class and instance, is the instance could ?only? be a global variable, and you don't really often want global objects unless they are static and/or Plain Old Data structures.

Roger Nelson