Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolon in namespace. Necessary?

Tags:

c++

namespaces

when working with namespace, I need to finish it with a semicolon? When I put a forward declaration of a class into a namespace, for example, many people doesn't include a semicolon but, it seems to be optional.

Does semicolon add functionality or change the current functionality by adding or removing?

Thanks.

like image 502
Killrazor Avatar asked Oct 03 '10 13:10

Killrazor


People also ask

Is semicolon mandatory in C?

Semicolon there is NOT optional.

Why do we need to give a semicolon after a structure declaration?

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. Doesn't even need the name or constituents: class { } waldo; is legal.

Do you need semicolon after class C++?

A semicolon after a close brace is mandatory if this is the end of a declaration. In case of braces, they have used in declarations of class, enum, struct, and initialization syntax. At the end of each of these statements, we need to put a semicolon.

Are namespaces necessary?

Namespaces provide a method for preventing name conflicts in large projects. Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes. Multiple namespace blocks with the same name are allowed.


2 Answers

If semicolon is optional it doesn't change functionality, otherwise it you omit it you'll get a syntax error.

namespace A {
    class B; // forward declaration, semicolon is mandatory.

    class B {
    }; // class definition, semicolon is mandatory

    class C {
    } f(); // because otherwise it is a return type of a function.
} // no need for semicolon

namespace D = A; // semicolon is mandatory.

If these are not the cases you talked about, comment please.

like image 129
Yakov Galka Avatar answered Oct 08 '22 10:10

Yakov Galka


No. Namespaces do not need to end with a semicolon though Bjarne wanted to do it I guess to reduce syntax related discrepancies with other C++ constructs. However I am not sure why it was not accepted.

"Silly typing errors will inevitably arise from the syntactic similarity of the namespace constructs to other C++ constructs. I propose we allow an optional semicolon after a global declaration to lessen the frustration. This would be a kind of ‘‘empty declaration’’ to match the empty statements."

All forward declarations of the class need to end with a semicolon. Can you give examples of where it is optional in C++?

like image 43
Chubsdad Avatar answered Oct 08 '22 10:10

Chubsdad