Killrazor
Killrazor

Reputation: 7184

Semicolon in namespace. Necessary?

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.

Upvotes: 16

Views: 11779

Answers (3)

Chubsdad
Chubsdad

Reputation: 25537

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++?

Upvotes: 8

Yakov Galka
Yakov Galka

Reputation: 72529

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.

Upvotes: 17

Peter Alexander
Peter Alexander

Reputation: 54300

No, you do not need to "finish it" with a semi-colon. It is not common practice, nor does it have any effect.

namespace foo
{
    ...
} // no semi-colon necessary here.

Upvotes: 3

Related Questions