Namespaces - cppreference.com

Namespaces provide a method for preventing name conflicts in large projects.

Entities declared inside a namespace block are placed in a namespace scope, which prevents them from being mistaken for identically-named entities in other scopes.

Entities declared outside all namespace blocks belong to the global namespace. The global namespace belongs to the

global scope

, and can be referred to explicitly with a leading ::. While it has no declaration, the global namespace is not an

unnamed namespace

.

Multiple namespace blocks with the same name are allowed. All declarations within these blocks are declared in the same namespace scope.

Syntax

namespacens-name{declarations}(1) inlinenamespacens-name{declarations}(2) (since C++11)namespace{declarations}(3) ns-name::member-name(4) usingnamespacens-name;(5) usingns-name::member-name;(6) namespacename=qualified-namespace;(7) namespacens-name::member-name{declarations}(8) (since C++17)namespacens-name::inlinemember-name{declarations}(9) (since C++20)1)

Named namespace definition

for the namespace ns-name.

2)

Inline namespace definition

for the namespace ns-name. Declarations inside ns-name 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

internal linkage

.

4) Namespace names (along with class names) can appear on the left hand side of the scope resolution operator, as part of

qualified name lookup

.

5)

using-directive

: From the point of view of unqualified

name lookup

of any name after a using-directive and until the end of the scope in which it appears, every name from ns-name is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and ns-name.

6)

using-declaration

: makes the symbol member-name from the namespace ns-name accessible for

unqualified lookup

as if declared in the same class scope, block scope, or namespace as where this using-declaration appears.

7)namespace-alias-definition: makes name a synonym for another namespace: see

namespace alias

8) nested namespace definition: namespaceA::B::C{...} is equivalent to namespaceA{namespaceB{namespaceC{...}}}.

9) nested inline namespace definition: namespaceA::B::inlineC{...} is equivalent to namespaceA::B{inlinenamespaceC{...}}. inline may appear in front of every namespace name except the first: namespaceA::inlineB::C{} is equivalent to namespaceA{inlinenamespaceB{namespaceC{}}}.

Explanation

Namespaces

inline(optional)namespaceattr(optional)identifier{ namespace-body} inline- (since C++11) if present, makes this an inline namespace (see below). Cannot appear on the extension-namespace-definition if the original-namespace-definition did not use inlineattr- (since C++17) optional sequence of any number of

attributes

identifier- either a previously unused identifier, in which case this is original-namespace-definition;

the name of a namespace, in which case this is extension-namespace-definition;

a sequence of enclosing namespace specifiers separated by ::, ending with identifier, in which case this is a nested-namespace-definition

(since C++17)namespace-body- possibly empty sequence of

declarations

of any kind (including class and function definitions as well as nested 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 identifier 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 namespace-body defines a

namespace scope

, which affects

name lookup

.

All names introduced by the declarations that appear within namespace-body (including nested namespace definitions) become members of the namespace identifier, whether this namespace definition is the original namespace definition (which introduced identifier), 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

namespaceQ{namespaceV// V is a member of Q, and is fully defined within Q{// namespace Q::V { // C++17 alternative to the lines aboveclassC{voidm();};// C is a member of V and is fully defined within V// C::m is only declaredvoidf();// f is a member of V, but is only declared here}voidV::f()// definition of V's member f outside of V// f's enclosing namespaces are still the global namespace, Q, and Q::V{externvoidh();// This declares ::Q::V::h}voidV::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).

Also, they must use qualified-id syntax.

namespaceQ{namespaceV// original-namespace-definition for V{voidf();// declaration of Q::V::f}voidV::f(){}// OKvoidV::g(){}// Error: g() is not yet a member of VnamespaceV// extension-namespace-definition for V{voidg();// declaration of Q::V::g}}namespaceR// not an enclosing namespace for Q{voidQ::V::g(){}// Error: cannot define Q::V::g inside R}voidQ::V::g(){}// OK: global namespace encloses QNames introduced by

friend

declarations within a non-local class X become members of the innermost enclosing namespace of X, but they do not become visible to ordinary

name lookup

(neither

unqualified

nor

qualified

) unless a matching declaration is provided at namespace scope, either before or after the class definition. Such name may be found through

ADL

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.

voidh(int);namespaceA{classX{friendvoidf(X);// A::f is a friendclassY{friendvoidg();// A::g is a friendfriendvoidh(int);// A::h is a friend, no conflict with ::h};};// A::f, A::g and A::h are not visible at namespace scope// even though they are members of the namespace AXx;voidg()// definition of A::g{f(x);// A::X::f is found through ADL}voidf(X){}// definition of A::fvoidh(int){}// definition of A::h// A::f, A::g and A::h are now visible at namespace scope// and they are also friends of A::X and A::X::Y}Inline namespaces

An inline namespace is a namespace that uses the optional keyword inline 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

argument-dependent lookup

, 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

name lookup

that examines the enclosing namespace will include the names from the inline namespaces even if the same name is present in the enclosing namespace.

// in C++14, std::literals and its member namespaces are inline{usingnamespacestd::string_literals;// makes visible operator""s // from std::literals::string_literalsautostr="abc"s;}{usingnamespacestd::literals;// makes visible both// std::literals::string_literals::operator""s// and std::literals::chrono_literals::operator""sautostr="abc"s;automin=60s;}{usingstd::operator""s;// makes both std::literals::string_literals::operator""s// and std::literals::chrono_literals::operator""s visibleautostr="abc"s;automin=60s;}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:

Run this code

namespaceLib{inlinenamespaceLib_1{template<typenameT>classA;}template<typenameT>voidg(T){/* ... */}}/* ... */structMyClass{/* ... */};namespaceLib{template<>classA<MyClass>{/* ... */};}intmain(){Lib::A<MyClass>a;g(a);// ok, Lib is an associated namespace of A}(since C++11)Unnamed namespaces

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

inline(optional)namespaceattr(optional){ namespace-body} inline- (since C++11) if present, makes this an inline namespace attr- (since C++17) optional sequence of any number of

attributes

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

qualified name lookup

and

unqualified name lookup

, but not for the

argument-dependent lookup

). 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.

namespace{inti;// defines ::(unique)::i}voidf(){i++;// increments ::(unique)::i}namespaceA{namespace{inti;// A::(unique)::iintj;// A::(unique)::j}voidg(){i++;}// A::(unique)::i++}usingnamespaceA;// introduces all names from A into global namespacevoidh(){i++;// error: ::(unique)::i and ::A::(unique)::i are both in scopeA::i++;// ok, increments ::A::(unique)::ij++;// ok, increments ::A::(unique)::j}Even though names in an unnamed namespace may be declared with external linkage, they are never accessible from other translation units because their namespace name is unique.

(until C++11)Unnamed namespaces as well as all namespaces declared directly or indirectly within an unnamed namespace have

internal linkage

, which means that any name that is declared within an unnamed namespace has internal linkage.

(since C++11)Using-declarations

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

usingtypename(optional)nested-name-specifierunqualified-id;(until C++17)usingdeclarator-list;(since C++17)typename- the keyword typename may be used as necessary to resolve

dependent names

, when the using-declaration introduces a member type from a base class into a class template nested-name-specifier- a sequence of names and scope resolution operators ::, ending with a scope resolution operator. A single :: refers to the global namespace. unqualified-id- an

id-expression

declarator-list- comma-separated list of one or more declarators of the form typename(optional)nested-name-specifierunqualified-id. A declarator may be followed by an ellipsis to indicate

pack expansion

, although that form is only meaningful in

derived class definitions

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, or to introduce

enumerators

into namespaces, block, and class scopes(since C++20).

A using-declaration with more than one using-declarator is equivalent to a corresponding sequence of using-declarations with one using-declarator.

(since C++17)For the use in derived class definitions, see

using declaration

.

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

voidf();namespaceA{voidg();}namespaceX{using::f;// global f is now visible as ::X::fusingA::g;// A::g is now visible as ::X::gusingA::g,A::g;// (C++17) OK: double declaration allowed at namespace scope}voidh(){X::f();// calls ::fX::g();// calls A::g}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

lookup

proceeds through the primary template.

namespaceA{voidf(int);}usingA::f;// ::f is now a synonym for A::f(int)namespaceA// namespace extension{voidf(char);// does not change what ::f means}voidfoo(){f('a');// calls f(int), even though f(char) exists.}voidbar(){usingA::f;// this f is a synonym for both A::f(int) and A::f(char)f('a');// calls f(char)}Using-declarations cannot name

template-id

, or namespace, or a scoped enumerator(until C++20). Each declarator in a using-declaration introduces one and only one name, for example using-declaration for an

enumeration

does not introduce any of its enumerators.

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

namespaceA{intx;}namespaceB{inti;structg{};structx{};voidf(int);voidf(double);voidg(char);// OK: function name g hides struct g}voidfunc(){inti;usingB::i;// error: i declared twicevoidf(char);usingB::f;// OK: f(char), f(int), f(double) are overloadsf(3.5);// calls B::f(double)usingB::g;g('a');// calls B::g(char)structgg1;// declares g1 to have type struct B::gusingB::x;usingA::x;// OK: hides struct B::xx=99;// assigns to A::xstructxx1;// declares x1 to have type struct B::x}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.

namespaceB{voidf(int);voidf(double);}namespaceC{voidf(int);voidf(double);voidf(char);}voidh(){usingB::f;// introduces B::f(int), B::f(double)usingC::f;// introduces C::f(int), C::f(double), and C::f(char)f('h');// calls C::f(char)f(1);// error: B::f(int) or C::f(int)?voidf(int);// error: f(int) conflicts with C::f(int) and B::f(int)}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:

namespaceX{namespaceM{voidg();// declares, but doesn't define X::M::g()}usingM::g;voidg();// Error: attempt to declare X::g which conflicts with X::M::g()}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

block-declaration

with the following syntax:

attr(optional)usingnamespacenamespace-name;(1) attr(optional)usingnamespacesplice-specifier;(2) (since C++26)attr- (since C++11) any number of

attributes

that apply to this using-directive namespace-name- a (possibly

qualified

) name of a namespace. When looking up this name,

lookup

considers namespace declarations only. It must not contain a

dependent

splice specifier(since C++26)splice-specifier- a

splice specifier

which must not be

dependent

Using-directives are allowed only in namespace

scope

and in block scope. From the point of view of

unqualified name lookup

of any name after a using-directive and until the end of the scope in which it appears, every name from namespace-name or splice-specifier(since C++26) is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and namespace-name or splice-specifier(since C++26).

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

unqualified lookup

: if a scope contains a using-directive that nominates a namespace-name, which itself contains using-directive for some namespace-name-2, 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.

namespaceA{inti;}namespaceB{inti;intj;namespaceC{namespaceD{usingnamespaceA;// Names from A are "injected" into D.// Unqualified lookup within D considers these names to have the same// scope as the global scope (e.g. for the purposes of name hiding).// Qualified lookup referring to D (D::name for some name)// will find the same name as unqualified lookup within D.intj;intk;inta=i;// i is B::i, because A::i is hidden by B::iintb=::i;// error: there is still no i in the global namespace}usingnamespaceD;// names from D and A are injected into Cintk=89;// OK to declare name identical to one introduced by a usingintl=k;// ambiguous: C::k or D::kintm=i;// ok: B::i hides A::iintn=j;// ok: D::j hides B::j}}// These are all equivalent definitions:intt0=B::i;intt1=B::C::a;intt2=B::C::D::a;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)

namespaceD{intd1;voidf(char);}usingnamespaceD;// introduces D::d1, D::f, D::d2, D::f,// E::e, and E::f into global namespace!intd1;// OK: no conflict with D::d1 when declaringnamespaceE{inte;voidf(int);}namespaceD// namespace extension{intd2;usingnamespaceE;// transitive using-directivevoidf(int);}voidf(){d1++;// error: ambiguous ::d1 or D::d1?::d1++;// OKD::d1++;// OKd2++;// OK, d2 is D::d2e++;// OK: e is E::e due to transitive usingf(1);// error: ambiguous: D::f(int) or E::f(int)?f('a');// OK: the only f(char) is D::f(char)}Notes

The using-directive usingnamespacestd; at any namespace scope introduces every name from the namespace std into the global namespace (since the global namespace is the nearest namespace that contains both std and any user-declared namespace), which may lead to undesirable name collisions. This, and other using directives are generally considered bad practice at file scope of a header file (

SF.7: Don’t write using namespace at global scope in a header file

).

Feature-test macroValueStdFeature

__cpp_namespace_attributes

201411L

(C++17)

Attributes

for namespaces Keywords

namespace

,

using

,

inline

Example

This example shows how to use a namespace to create a class that already has been named in the std namespace.

Run this code

#include<vector>namespacevec{template<typenameT>classvector{// ...};}// of vecintmain(){std::vector<int>v1;// Standard vector.vec::vector<int>v2;// User defined vector.// v1 = v2; // Error: v1 and v2 are different object's type.{usingnamespacestd;vector<int>v3;// Same as std::vectorv1=v3;// OK}{usingvec::vector;vector<int>v4;// Same as vec::vectorv2=v4;// OK}}Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

CWG 101

C++98 the program is ill-formed if a function declaration in namespace
scope or block scope and a function introduced by a
using-declaration declare the same function (no ambiguity) allowed

CWG 373

C++98 lookup only considered namespace declarations only for
the last name in the operand of a using-directive (which is
sub-optimal, because classes cannot contain namespaces) the lookup restriction
applies to all names in the
operands of using-directives

CWG 460

C++98 a using-declaration could name a namespace prohibited

CWG 565

C++98 a using-declaration cannot introduce a function
identical to another function in the same scope, but
the restriction was not applied to function templates apply the same restriction
to function templates as well

CWG 986

C++98 using-directive was transitive for qualified lookup only transitive for unqualified lookup

CWG 987

C++98 entities declared in a nested namespace was
also members of the enclosing namespace nested scopes excluded

CWG 1021

C++98 it was unclear whether an entity whose definition
is introduced to a namespace via using-declaration
is considered to be defined in that namespace not defined in that namespace

CWG 1838

C++98 unqualified definition in an outer namespace
could define an entity declared, but not defined in
another namespace and pulled in by a using unqualified definition
always refers to
its namespace

CWG 2155

C++98 the resolution of

CWG issue 1838

was not
applied to class and enumeration declarations applied See also