[namespace.def.general] # 9 Declarations [[dcl]](./#dcl) ## 9.9 Namespaces [[basic.namespace]](basic.namespace#namespace.def.general) ### 9.9.2 Namespace definition [[namespace.def]](namespace.def#general) #### 9.9.2.1 General [namespace.def.general] [namespace-name:](#nt:namespace-name "9.9.2.1 General [namespace.def.general]") [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [*namespace-alias*](namespace.alias#nt:namespace-alias "9.9.3 Namespace alias [namespace.alias]") [namespace-definition:](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") [*named-namespace-definition*](#nt:named-namespace-definition "9.9.2.1 General [namespace.def.general]") [*unnamed-namespace-definition*](#nt:unnamed-namespace-definition "9.9.2.1 General [namespace.def.general]") [*nested-namespace-definition*](#nt:nested-namespace-definition "9.9.2.1 General [namespace.def.general]") [named-namespace-definition:](#nt:named-namespace-definition "9.9.2.1 General [namespace.def.general]") inlineopt namespace [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")opt [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") { [*namespace-body*](#nt:namespace-body "9.9.2.1 General [namespace.def.general]") } [unnamed-namespace-definition:](#nt:unnamed-namespace-definition "9.9.2.1 General [namespace.def.general]") inlineopt namespace [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")opt { [*namespace-body*](#nt:namespace-body "9.9.2.1 General [namespace.def.general]") } [nested-namespace-definition:](#nt:nested-namespace-definition "9.9.2.1 General [namespace.def.general]") namespace [*enclosing-namespace-specifier*](#nt:enclosing-namespace-specifier "9.9.2.1 General [namespace.def.general]") :: inlineopt [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") { [*namespace-body*](#nt:namespace-body "9.9.2.1 General [namespace.def.general]") } [enclosing-namespace-specifier:](#nt:enclosing-namespace-specifier "9.9.2.1 General [namespace.def.general]") [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [*enclosing-namespace-specifier*](#nt:enclosing-namespace-specifier "9.9.2.1 General [namespace.def.general]") :: inlineopt [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [namespace-body:](#nt:namespace-body "9.9.2.1 General [namespace.def.general]") [*declaration-seq*](dcl.pre#nt:declaration-seq "9.1 Preamble [dcl.pre]")opt [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8270) Every [*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") shall inhabit a namespace scope ([[basic.scope.namespace]](basic.scope.namespace "6.4.6 Namespace scope"))[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8273) In a [*named-namespace-definition*](#nt:named-namespace-definition "9.9.2.1 General [namespace.def.general]") D, the [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") is the name of the namespace[.](#2.sentence-1) The [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") is looked up by searching for it in the scopes of the namespace A in which D appears and of every element of the inline namespace set of A[.](#2.sentence-2) If the lookup finds a [*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") for a namespace N,D [*extends*](#def:namespace,extend "9.9.2.1 General [namespace.def.general]") N, and the target scope of D is the scope to which N belongs[.](#2.sentence-3) If the lookup finds nothing, the [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") is introduced as a [*namespace-name*](#nt:namespace-name "9.9.2.1 General [namespace.def.general]") into A[.](#2.sentence-4) [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8287) Because a [*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") contains[*declaration*](dcl.pre#nt:declaration "9.1 Preamble [dcl.pre]")*s* in its [*namespace-body*](#nt:namespace-body "9.9.2.1 General [namespace.def.general]") and a[*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") is itself a [*declaration*](dcl.pre#nt:declaration "9.1 Preamble [dcl.pre]"), it follows that [*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]")*s* can be nested[.](#3.sentence-1) [*Example [1](#example-1)*: namespace Outer {int i; namespace Inner {void f() { i++; } // Outer​::​iint i; void g() { i++; } // Inner​::​i}} — *end example*] [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8305) If the optional initial inline keyword appears in a[*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") for a particular namespace, that namespace is declared to be an [*inline namespace*](#def:namespace,inline "9.9.2.1 General [namespace.def.general]")[.](#4.sentence-1) The inline keyword may be used on a [*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") that extends a namespace only if it was previously used on the [*namespace-definition*](#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") that initially declared the [*namespace-name*](#nt:namespace-name "9.9.2.1 General [namespace.def.general]") for that namespace[.](#4.sentence-2) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8313) The optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]") in a [*named-namespace-definition*](#nt:named-namespace-definition "9.9.2.1 General [namespace.def.general]") appertains to the namespace being defined or extended[.](#5.sentence-1) [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8318) Members of an inline namespace can be used in most respects as though they were members of the innermost enclosing namespace[.](#6.sentence-1) Specifically, the inline namespace and its enclosing namespace are both added to the set of associated namespaces used in[argument-dependent lookup](basic.lookup.argdep "6.5.4 Argument-dependent name lookup [basic.lookup.argdep]") whenever one of them is, and a [*using-directive*](namespace.udir#nt:using-directive "9.9.4 Using namespace directive [namespace.udir]") ([[namespace.udir]](namespace.udir "9.9.4 Using namespace directive")) that names the inline namespace is implicitly inserted into the enclosing namespace as for an unnamed namespace ([[namespace.unnamed]](namespace.unnamed "9.9.2.2 Unnamed namespaces"))[.](#6.sentence-2) Furthermore, each member of the inline namespace can subsequently be partially specialized ([[temp.spec.partial]](temp.spec.partial "13.7.6 Partial specialization")), explicitly instantiated ([[temp.explicit]](temp.explicit "13.9.3 Explicit instantiation")), or explicitly specialized ([[temp.expl.spec]](temp.expl.spec "13.9.4 Explicit specialization")) as though it were a member of the enclosing namespace[.](#6.sentence-3) Finally, looking up a name in the enclosing namespace via explicit qualification ([[namespace.qual]](namespace.qual "6.5.5.3 Namespace members")) will include members of the inline namespace even if there are declarations of that name in the enclosing namespace[.](#6.sentence-4) [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8334) 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[.](#7.sentence-1) The [*inline namespace set*](#def:inline_namespace_set "9.9.2.1 General [namespace.def.general]") of N is the transitive closure of all inline namespaces in N[.](#7.sentence-2) [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L8341) A [*nested-namespace-definition*](#nt:nested-namespace-definition "9.9.2.1 General [namespace.def.general]") with an[*enclosing-namespace-specifier*](#nt:enclosing-namespace-specifier "9.9.2.1 General [namespace.def.general]") E,[*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") I and[*namespace-body*](#nt:namespace-body "9.9.2.1 General [namespace.def.general]") B is equivalent tonamespace E { inlineopt namespace I { B } } where the optional inline is present if and only if the [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") I is preceded by inline[.](#8.sentence-1) [*Example [2](#example-2)*: 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*]