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.
Semicolon there is NOT optional.
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.
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.
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.
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.
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++?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With