views:

107

answers:

3

Hello,

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.

A: 

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.
Peter Alexander
+3  A: 

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.

ybungalobill
When you say "no need for semicolon", any semicolon is illegal in C++03.
Charles Bailey
@Charles: Are you sure? In C++0x and C++98 *simple-declaration* can degenerate to `;` (everything else marked *opt*).
ybungalobill
Yes, check 7 [dcl.dcl] paragraph 3. In a _simple-declaration_ the optional init-declarator-list can be omitted only when declaring a class or enumeration, ... . Basically, the _init-declarator-list_ can be omitted only when _decl-specifier-seq_ isn't.
Charles Bailey
It is, of course, legal in C++0x as there is a new type of _declaration_, the _empty-declaration_ which consists of just a `;` and explicitly has no effect. Personally, I don't see the point of using an _empty-declaration_.
Charles Bailey
@Charles: thanks for the info. You may use it when you generate code with e.g. macros and sometimes it expands to nothing.
ybungalobill
@ybungalobill: I'm not sure I can think of an example where it would be necessary to generate a single (only legal in the future) `;` instead of nothing. Or did I misunderstand what you mean?
Charles Bailey
It's marginally useful, as it makes `int a;;` legal. Consider a macro `#define DECLARE_MEMBER(x) int x;`, unintentionally used as `DECLARE_MEMBER(a);`
MSalters
+1  A: 

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

Chubsdad
I think that bruce eckel recommends to end with a semicolon to enforce the idea of an isolated block
Killrazor