cpp/language/namespace

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. All declarations within those blocks are declared in the named scope.

Syntax
@1@ Named namespace definition for the namespace. @2@ Inline namespace definition for the namespace. Declarations inside will be visible in its enclosing namespace. @3@ Unnamed namespace definition. Its members have potential scope from their point of declaration to the end of the translation unit, and have. @4@ Namespace names (along with class names) can appear on the left hand side of the scope resolution operator, as part of. @5@ using-directive: From the point of view of unqualified of any name after a using-directive and until the end of the scope in which it appears, every name from  is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and. @6@ using-declaration: makes the symbol from the namespace  accessible for  as if declared in the same class scope, block scope, or namespace as where this using-declaration appears. @7@ : makes a synonym for another namespace: see @8@ nested namespace definition: is equivalent to. @9@ nested inline namespace definition: is equivalent to. may appear in front of every namespace name except the first: is equivalent to.

Namespaces
Namespace definitions are only allowed at namespace scope, including the global scope.

To reopen an existing namespace (formally, to be an extension-namespace-definition), the lookup for the used in the namespace definition must resolve to a namespace name (not a namespace alias), that was declared as a member of the enclosing namespace or of an inline namespace within an enclosing namespace.

The defines a, which affects.

All names introduced by the declarations that appear within (including nested namespace definitions) become members of the namespace, whether this namespace definition is the original namespace definition (which introduced ), or an extension namespace definition (which "reopened" the already defined namespace)

A namespace member that was declared within a namespace body may be defined or redeclared outside of it using explicit qualification {{source|1= namespace Q { namespace V  // V is a member of Q, and is fully defined within Q    { // namespace Q::V { // C++17 alternative to the lines above class C { void m; }; // C is a member of V and is fully defined within V                              // C::m is only declared void f; // f is a member of V, but is only declared here }   void V::f // definition of V's member f outside of V                // f's enclosing namespaces are still the global namespace, Q, and Q::V {       extern void h; // This declares ::Q::V::h }   void V::C::m // definition of V::C::m outside of the namespace (and the class body) // enclosing namespaces are the global namespace, Q, and Q::V {} } }}

Out-of-namespace definitions and redeclarations are only allowed
 * after the point of declaration,
 * at namespace scope, and
 * in namespaces that enclose the original namespace (including the global namespace).

Names introduced by declarations within a non-local class X become members of the innermost enclosing namespace of X, but they do not become visible to ordinary  (neither  nor ) unless a matching declaration is provided at namespace scope, either before or after the class definition. Such name may be found through which considers both namespaces and classes.

Only the innermost enclosing namespace is considered by such friend declaration when deciding whether the name would conflict with a previously declared name.

{{rrev|since=c++11|

Inline namespaces
An inline namespace is a namespace that uses the optional keyword in its original-namespace-definition.

Members of an inline namespace are treated as if they are members of the enclosing namespace in many situations (listed below). This property is transitive: if a namespace N contains an inline namespace M, which in turn contains an inline namespace O, then the members of O can be used as though they were members of M or N.


 * A using-directive that names the inline namespace is implicitly inserted in the enclosing namespace (similar to the implicit using-directive for the unnamed namespace)
 * In, when a namespace is added to the set of associated namespaces, its inline namespaces are added as well, and if an inline namespace is added to the list of associated namespaces, its enclosing namespace is added as well.
 * Each member of an inline namespace can be partially specialized, explicitly instantiated, or explicitly specialized as if it were a member of the enclosing namespace.
 * Qualified that examines the enclosing namespace will include the names from the inline namespaces even if the same name is present in the enclosing namespace.

Note: the rule about specializations allows library versioning: different implementations of a library template may be defined in different inline namespaces, while still allowing the user to extend the parent namespace with an explicit specialization of the primary template:

}}

Unnamed namespaces
The unnamed-namespace-definition is a namespace definition of the form

This definition is treated as a definition of a namespace with unique name and a using-directive in the current scope that nominates this unnamed namespace (Note: implicitly added using directive makes namespace available for the and, but not for the ). The unique name is unique over the entire program, but within a translation unit each unnamed namespace definition maps to the same unique name: multiple unnamed namespace definitions in the same scope denote the same unnamed namespace.

Using-declarations
Introduces a name that is defined elsewhere into the declarative region where this using-declaration appears.

Using-declarations can be used to introduce namespace members into other namespaces and block scopes, or to introduce base class members into derived class definitions.

For the use in derived class definitions, see.

Names introduced into a namespace scope by a using-declaration can be used just like any other names, including qualified lookup from other scopes:

If, after the using-declaration was used to take a member from a namespace, the namespace is extended and additional declarations for the same name are introduced, those additional declarations do not become visible through the using-declaration (in contrast with using-directive). One exception is when a using-declaration names a class template: partial specializations introduced later are effectively visible, because their proceeds through the primary template.

Using-declarations cannot name, or namespace. Each declarator in a using-declaration introduces one and only one name, for example using-declaration for an does not introduce any of its enumerators.

All restrictions on regular declarations of the same names, hiding, and overloading rules apply to using-declarations:

If a function was introduced by a using-declaration, declaring a function with the same name and parameter list is ill-formed (unless the declaration is for the same function). If a function template was introduced by a using-declaration, declaring a function template with the same name, parameter type list, return type, and template parameter list is ill-formed. Two using-declarations can introduce functions with the same name and parameter list, but if a call to that function is attempted, the program is ill-formed.

If an entity is declared, but not defined in some inner namespace, and then declared through using-declaration in the outer namespace, and then a definition appears in the outer namespace with the same unqualified name, that definition is a member of the outer namespace and conflicts with the using-declaration:

More generally, a declaration that appears in any namespace scope and introduces a name using an unqualified identifier always introduces a member into the namespace it's in and not to any other namespace. The exceptions are explicit instantiations and explicit specializations of a primary template that is defined in an inline namespace: because they do not introduce a new name, they may use unqualified-id in an enclosing namespace.

Using-directives
A using-directive is a with the following syntax:

Using-directives are allowed only in namespace and in block scope. From the point of view of of any name after a using-directive and until the end of the scope in which it appears, every name from  is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and.

Using-directive does not add any names to the declarative region in which it appears (unlike the using-declaration), and thus does not prevent identical names from being declared.

Using-directives are transitive for the purposes of : if a scope contains a using-directive that nominates a, which itself contains using-directive for some , the effect is as if the using directives from the second namespace appear within the first. The order in which these transitive namespaces occur does not influence name lookup.

If, after a using-directive was used to nominate some namespace, the namespace is extended and additional members and/or using-directives are added to it, those additional members and the additional namespaces are visible through the using-directive (in contrast with using-declaration)