The standard does not allow code like this:
namespace Hello::World { //Things that are in namespace Hello::World }
and instead requires
namespace Hello { namespace World { //Things that are in namespace Hello::World }}
What is the rationale? Was this simply not thought of at the time, or is there a specific reason it is not included?
It seems that the first syntax more directly expresses in which namespace one is supposed to be, as the declaration mimics the actual use of the namespace in later code. It also results in less indentation if you are unfortunate enough to be using a "dumb" bracket counting indentation tool.
In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace.
No, C does not have a namespace mechanism whereby you can provide “module-like data hiding”.
Use a using directive in an implementation file (i.e. *. cpp) if you are using several different identifiers in a namespace; if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace.
An anonymous namespace makes the enclosed variables, functions, classes, etc. available only inside that file. In your example it's a way to avoid global variables. There is no runtime or compile time performance difference.
The reason is most likely "because that's how the language evolved."
There has been at least one proposal ("Nested Namespace Definition Proposal" in 2003) to allow nested namespace definitions, but it was not selected for inclusion in C++0x.
Nested namespace definition is part of the C++17 working draft.
This topic is mentioned in the proposal as an example of this feature being a programmers' demand original n4026 updated version: n4230.
Current latest draft: n4567 (paragraph 7.3.1 item 10)
7.3.1 Namespace Definition
...
10 A nested-namespace-definition with an enclosing-namespace-specifier E, identifier I and namespace-body B is equivalent tonamespace E { namespace I { B } }
Example:
namespace A::B::C { int i; } The above has the same effect as: namespace A { namespace B { namespace C { int i; } } }
GCC since version 6 enable using -std=c++1z
Visual C++ since 2015 update 3 enable using /std:c++latest
Clang since version 3.6 enable using -std=c++1z
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