Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a specific reason nested namespace declarations are not allowed in C++?

Tags:

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.

like image 348
Billy ONeal Avatar asked Aug 30 '10 19:08

Billy ONeal


People also ask

Can namespace be nested?

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.

Does C support namespace?

No, C does not have a namespace mechanism whereby you can provide “module-like data hiding”.

What are is difference between using namespace directive and using the using declaration for accessing namespace members?

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.

What is the use of unnamed 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.


2 Answers

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.

like image 99
James McNellis Avatar answered Oct 25 '22 19:10

James McNellis


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 to namespace 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;     }   } } 

Compiler Support

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

like image 22
Farway Avatar answered Oct 25 '22 21:10

Farway