Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolon after class declaration braces

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; 

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.

like image 640
SmacL Avatar asked Apr 24 '09 12:04

SmacL


People also ask

Can you put a semicolon after a bracket?

Sometimes the parenthetical material is part of a longer sentence part that will be set off by a comma, colon, or semicolon. These pieces of punctuation always come after the parenthetical material, never before it or inside the parentheses.

Do you need semicolon after curly braces Javascript?

Avoid! You shouldn't put a semicolon after a closing curly bracket } . The only exceptions are assignment statements, such as var obj = {}; , see above. It won't harm to put a semicolon after the { } of an if statement (it will be ignored, and you might see a warning that it's unnecessary).

What is the use of semicolon and curly braces?

Semicolons go at the end of lines that do not end in a curly brace or to separate statements on the same line. It does no harm to use them after a closing brace, or to wear suspenders and a belt, but it does look a little nerdy.

Why use a semicolon after a struct?

struct MyStruct { } anInstanceOfMyStruct; struct { } anInstanceOfAnUnnamedStruct; You need the semicolon to indicate you aren't creating a new instance. The language designers of C# and Java apparently didn't feel that allowing this was a useful addition to their language.


2 Answers

The link provided by @MichaelHaren appears to provide the root cause. The semicolon (as others have pointed out) is inherited from C. But that doesn't explain why C used it in the first place. The discussion includes this gem of an example:

struct fred { int x; long y; };  main()  {    return 0;  }  

Older versions of C had an implicit int return type from a function unless declared otherwise. If we omit the ; at the end of the structure definition, we're not only defining a new type fred, but also declaring that main() will return an instance of fred. I.e. the code would be parsed like this:

struct fred { int x; long y; } main() {    return 0; /* invalid return type, expected fred type */ }  
like image 76
Nathan Avatar answered Oct 18 '22 14:10

Nathan


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.

like image 29
JaredPar Avatar answered Oct 18 '22 13:10

JaredPar