[namespace.def] (original) (raw)

9 Declarations [dcl.dcl]

9.8 Namespaces [basic.namespace]

9.8.2 Namespace definition [namespace.def]

9.8.2.1 General [namespace.def.general]

In a named-namespace-definition, the identifier is the name of the namespace.

If the identifier, when looked up, refers to a namespace-name (but not a namespace-alias) that was introduced in the namespace in which the named-namespace-definition appears or that was introduced in a member of the inline namespace set of that namespace, the namespace-definition extends the previously-declared namespace.

Otherwise, the identifier is introduced as a namespace-name into the declarative region in which the named-namespace-definition appears.

Because a namespace-definition containsdeclarations in its namespace-body and anamespace-definition is itself a declaration, it follows that namespace-definitions can be nested.

[Example 1: namespace Outer { int i;namespace Inner { void f() { i++; } int i;void g() { i++; } } } — _end example_]

The enclosing namespaces of a declaration are those namespaces in which the declaration lexically appears, except for a redeclaration of a namespace member outside its original namespace (e.g., a definition as specified in [namespace.memdef]).

Such a redeclaration has the same enclosing namespaces as the original declaration.

[Example 2: namespace Q { namespace V { void f(); class C { void m(); };} void V::f() { extern void h(); } void V::C::m() { } } — _end example_]

If the optional initial inline keyword appears in anamespace-definition for a particular namespace, that namespace is declared to be an inline namespace.

The inline keyword may be used on a namespace-definition that extends a namespace only if it was previously used on the namespace-definitionthat initially declared the namespace-name for that namespace.

Members of an inline namespace can be used in most respects as though they were members of the enclosing namespace.

Specifically, the inline namespace and its enclosing namespace are both added to the set of associated namespaces used inargument-dependent lookup whenever one of them is, and a using-directive ([namespace.udir]) that names the inline namespace is implicitly inserted into the enclosing namespace as for an unnamed namespace.

Finally, looking up a name in the enclosing namespace via explicit qualification ([namespace.qual]) will include members of the inline namespace brought in by the using-directive even if there are declarations of that name in the enclosing namespace.

These properties are transitive: if a namespace N contains an inline namespaceM, which in turn contains an inline namespace O, then the members ofO can be used as though they were members of M or N.

The inline namespace set of N is the transitive closure of all inline namespaces in N.

The enclosing namespace set of O is the set of namespaces consisting of the innermost non-inline namespace enclosing an inline namespace O, together with any intervening inline namespaces.

A nested-namespace-definition with anenclosing-namespace-specifier E,identifier I andnamespace-body Bis equivalent tonamespace E { inline namespace I { B } } where the optional inline is present if and only if the identifier I is preceded by inline.

[Example 3: namespace A::inline B::C { int i;}

The above has the same effect as:namespace A { inline namespace B { namespace C { int i;} } }

— _end example_]

9.8.2.2 Unnamed namespaces [namespace.unnamed]

An unnamed-namespace-definition behaves as if it were replaced by

inline namespace unique { }
using namespace unique ;
namespace unique { namespace-body }

whereinline appears if and only if it appears in theunnamed-namespace-definitionand all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the translation unit.

[Example 1: namespace { int i; } void f() { i++; } namespace A { namespace { int i; int j; } void g() { i++; } } using namespace A;void h() { i++; A::i++; j++; } — _end example_]

9.8.2.3 Namespace member definitions [namespace.memdef]

[Note 1:

An explicit instantiation ([temp.explicit]) or explicit specialization ([temp.expl.spec]) of a template does not introduce a name and thus can be declared using anunqualified-id in a member of the enclosing namespace set, if the primary template is declared in an inline namespace.

— _end note_]

[Example 1: namespace X { void f() { } namespace M { void g(); } using M::g;void g(); } — _end example_]

Members of a named namespace can also be defined outside that namespace by explicit qualification ([namespace.qual]) of the name being defined, provided that the entity being defined was already declared in the namespace and the definition appears after the point of declaration in a namespace that encloses the declaration's namespace.

[Example 2: namespace Q { namespace V { void f();} void V::f() { } void V::g() { } namespace V { void g();} } namespace R { void Q::V::g() { } } — _end example_]

If a friend declaration in a non-local class first declares a class, function, class template or function template99the friend is a member of the innermost enclosing namespace.

[Note 2:

The name of the friend will be visible in its namespace if a matching declaration is provided at namespace scope (either before or after the class definition granting friendship).

— _end note_]

If a friend function or function template is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments ([basic.lookup.argdep]).

If the name in a friend declaration is neither qualified nor atemplate-id and the declaration is a function or anelaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.

[Note 3:

The other forms of friend declarations cannot declare a new member of the innermost enclosing namespace and thus follow the usual lookup rules.

— _end note_]

[Example 3: void h(int);template <class T> void f2(T);namespace A { class X { friend void f(X); class Y { friend void g(); friend void h(int); friend void f2<>(int); };}; X x;void g() { f(x); } void f(X) { } void h(int) { } } using A::x;void h() { A::f(x); A::X::f(x); A::X::Y::g(); } — _end example_]