namespace-name: identifier namespace-alias
namespace-definition: named-namespace-definition unnamed-namespace-definition nested-namespace-definition
named-namespace-definition: inline namespace attribute-specifier-seq identifier { namespace-body }
unnamed-namespace-definition: inline namespace attribute-specifier-seq { namespace-body }
nested-namespace-definition: namespace enclosing-namespace-specifier :: inline identifier { namespace-body }
enclosing-namespace-specifier: identifier enclosing-namespace-specifier :: inline identifier
namespace-body: declaration-seq
namespace Outer { int i; namespace Inner { void f() { i++; } // Outerโ::โi int i; void g() { i++; } // Innerโ::โi } }โ end example
namespace Q { namespace V { void f(); // enclosing namespaces are the global namespace, Q, and Qโ::โV class C { void m(); }; } void V::f() { // enclosing namespaces are the global namespace, Q, and Qโ::โV extern void h(); // ... so this declares Qโ::โVโ::โh } void V::C::m() { // enclosing namespaces are the global namespace, Q, and Qโ::โV } }โ end example
namespace E { inline namespace I { B } }where the optional inline is present if and only if the identifier I is preceded by inline.
namespace A::inline B::C { int i; }
namespace A { inline namespace B { namespace C { int i; } } }
inline namespace unique { /* empty body */ } using namespace unique ; namespace unique { namespace-body }where inline appears if and only if it appears in the unnamed-namespace-definition and 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.
namespace { int i; } // uniqueโ::โi void f() { i++; } // uniqueโ::โi++ namespace A { namespace { int i; // Aโ::โuniqueโ::โi int j; // Aโ::โuniqueโ::โj } void g() { i++; } // Aโ::โuniqueโ::โi++ } using namespace A; void h() { i++; // error: uniqueโ::โi or Aโ::โuniqueโ::โi A::i++; // Aโ::โuniqueโ::โi j++; // Aโ::โuniqueโ::โj }โ end example
namespace X { void f() { /* ... */ } // OK: introduces Xโ::โf() namespace M { void g(); // OK: introduces Xโ::โMโ::โg() } using M::g; void g(); // error: conflicts with Xโ::โMโ::โg() }โ end example
namespace Q { namespace V { void f(); } void V::f() { /* ... */ } // OK void V::g() { /* ... */ } // error: g() is not yet a member of V namespace V { void g(); } } namespace R { void Q::V::g() { /* ... */ } // error: R doesn't enclose Q }โ end example
// Assume f and g have not yet been declared. void h(int); template <class T> void f2(T); namespace A { class X { friend void f(X); // Aโ::โf(X) is a friend class Y { friend void g(); // Aโ::โg is a friend friend void h(int); // Aโ::โh is a friend // โ::โh not considered friend void f2<>(int); // โ::โf2<>(int) is a friend }; }; // Aโ::โf, Aโ::โg and Aโ::โh are not visible here X x; void g() { f(x); } // definition of Aโ::โg void f(X) { /* ... */ } // definition of Aโ::โf void h(int) { /* ... */ } // definition of Aโ::โh // Aโ::โf, Aโ::โg and Aโ::โh are visible here and known to be friends } using A::x; void h() { A::f(x); A::X::f(x); // error: f is not a member of Aโ::โX A::X::Y::g(); // error: g is not a member of Aโ::โXโ::โY }โ end example