Description
Namespaces are used in C++ to group together declarations of functions and types. The members of a namespace are not automatically available outside the namespace scope. They can be referenced by prefacing the member names with N::, where N is the namespace name.
There are also a couple of other ways of referencing members within a namespace. One is to specify a using-directive:
using namespace N;meaning that all the members of namespace N are available. This declaration is passive, that is, the members are made available, but identical members from two namespaces do not conflict unless they are actually used. For example:
namespace A { void f(); } namespace B { void f(); } using namespace A; using namespace B; void g() { f(); // ambiguity A::f(); // OK B::f(); // OK }
Another way that namespace members can be accessed is via an individual using-declaration for a specific member. For example:
namespace A { void f() {} void g() {} } using A::f;This technique is used to select individual members from a namespace.
Namespace aliases, of the form:
namespace B = A;are used to give a new alias name (B) for an existing namespace name (A). They can be used to give short aliases to long namespace names, or to reference a versioned "library" of declarations by saying:
namespace Lib = Core_Library_version4_7; ... Lib::T x;
Concept
The sample defines two namespaces, and sets up an alias A_ALIAS for the first one. The members of the first namespace are exported via using namespace A. Only the g() member of the second namespace is exported. If we had exported all of B, then the call to f() would result in an ambiguity error.
Supported
Supported
Supported