[basic.scope] # 6 Basics [[basic]](./#basic) ## 6.4 Scope [basic.scope] ### [6.4.1](#scope) General [[basic.scope.scope]](basic.scope.scope) [1](#scope-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L939) The declarations in a program appear in a number of [*scopes*](#def:scope "6.4.1 General [basic.scope.scope]") that are in general discontiguous[.](#scope-1.sentence-1) The [*global scope*](#def:scope,global "6.4.1 General [basic.scope.scope]") contains the entire program; every other scope S is introduced by a declaration,[*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6 Functions [dcl.fct]"),[*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]"),[*handler*](except.pre#nt:handler "14.1 Preamble [except.pre]"), or contract assertion (as described in the following subclauses of [basic.scope]) appearing in another scope, which thereby contains S[.](#scope-1.sentence-2) An [*enclosing scope*](#def:scope,enclosing "6.4.1 General [basic.scope.scope]") at a program point is any scope that contains it; the smallest such scope is said to be the [*immediate scope*](#def:scope,immediate "6.4.1 General [basic.scope.scope]") at that point[.](#scope-1.sentence-3) A scope [*intervenes*](#def:scope,intervene "6.4.1 General [basic.scope.scope]") between a program point P and a scope S (that does not contain P) if it is or contains S but does not contain P[.](#scope-1.sentence-4) [2](#scope-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L958) Unless otherwise specified: - [(2.1)](#scope-2.1) The smallest scope that contains a scope S is the [*parent scope*](#def:scope,parent "6.4.1 General [basic.scope.scope]") of S[.](#scope-2.1.sentence-1) - [(2.2)](#scope-2.2) No two declarations (re)introduce the same entity[.](#scope-2.2.sentence-1) - [(2.3)](#scope-2.3) A declaration [*inhabits*](#def:scope,inhabit "6.4.1 General [basic.scope.scope]") the immediate scope at its locus ([[basic.scope.pdecl]](#pdecl "6.4.2 Point of declaration"))[.](#scope-2.3.sentence-1) - [(2.4)](#scope-2.4) A declaration's [*target scope*](#def:scope,target "6.4.1 General [basic.scope.scope]") is the scope it inhabits[.](#scope-2.4.sentence-1) - [(2.5)](#scope-2.5) Any names (re)introduced by a declaration are [*bound*](#def:name,bound "6.4.1 General [basic.scope.scope]") to it in its target scope[.](#scope-2.5.sentence-1) The [*host scope*](#def:scope,host "6.4.1 General [basic.scope.scope]") of a declaration is the inhabited scope if that scope is a block scope and the target scope otherwise[.](#scope-2.sentence-2) An entity [*belongs*](#def:entity,belong "6.4.1 General [basic.scope.scope]") to a scope S if S is the target scope of a declaration of the entity[.](#scope-2.sentence-3) [*Note [1](#scope-note-1)*: Special cases include that: - [(2.6)](#scope-2.6) Template parameter scopes are parents only to other template parameter scopes ([[basic.scope.temp]](#temp "6.4.9 Template parameter scope"))[.](#scope-2.6.sentence-1) - [(2.7)](#scope-2.7) Corresponding declarations with appropriate linkage declare the same entity ([[basic.link]](basic.link "6.7 Program and linkage"))[.](#scope-2.7.sentence-1) - [(2.8)](#scope-2.8) The declaration in a [*template-declaration*](temp.pre#nt:template-declaration "13.1 Preamble [temp.pre]") inhabits the same scope as the [*template-declaration*](temp.pre#nt:template-declaration "13.1 Preamble [temp.pre]")[.](#scope-2.8.sentence-1) - [(2.9)](#scope-2.9) Friend declarations and declarations of template specializations do not bind names ([[dcl.meaning]](dcl.meaning "9.3.4 Meaning of declarators")); those with qualified names target a specified scope, and other friend declarations and certain [*elaborated-type-specifier*](dcl.type.elab#nt:elaborated-type-specifier "9.2.9.5 Elaborated type specifiers [dcl.type.elab]")*s* ([[dcl.type.elab]](dcl.type.elab "9.2.9.5 Elaborated type specifiers")) target a larger enclosing scope[.](#scope-2.9.sentence-1) - [(2.10)](#scope-2.10) Block-scope extern or function declarations target a larger enclosing scope but bind a name in their immediate scope ([[dcl.meaning.general]](dcl.meaning.general "9.3.4.1 General"))[.](#scope-2.10.sentence-1) - [(2.11)](#scope-2.11) The names of unscoped enumerators are bound in the two innermost enclosing scopes ([[dcl.enum]](dcl.enum "9.8.1 Enumeration declarations"))[.](#scope-2.11.sentence-1) - [(2.12)](#scope-2.12) A class's name is also bound in its own scope ([[class.pre]](class.pre "11.1 Preamble"))[.](#scope-2.12.sentence-1) - [(2.13)](#scope-2.13) The names of the members of an anonymous union are bound in the union's parent scope ([[class.union.anon]](class.union.anon "11.5.2 Anonymous unions"))[.](#scope-2.13.sentence-1) — *end note*] [3](#scope-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1013) Two non-static member functions have[*corresponding object parameters*](#def:object_parameter,corresponding "6.4.1 General [basic.scope.scope]") if - [(3.1)](#scope-3.1) exactly one is an implicit object member function with no [*ref-qualifier*](dcl.decl.general#nt:ref-qualifier "9.3.1 General [dcl.decl.general]") and the types of their object parameters ([[dcl.fct]](dcl.fct "9.3.4.6 Functions")), after removing references, are the same, or - [(3.2)](#scope-3.2) their object parameters have the same type[.](#scope-3.sentence-1) Two non-static member function templates have[*corresponding object parameters*](#def:object_parameter,corresponding "6.4.1 General [basic.scope.scope]") if - [(3.3)](#scope-3.3) exactly one is an implicit object member function with no [*ref-qualifier*](dcl.decl.general#nt:ref-qualifier "9.3.1 General [dcl.decl.general]") and the types of their object parameters, after removing any references, are equivalent, or - [(3.4)](#scope-3.4) the types of their object parameters are equivalent[.](#scope-3.sentence-2) Two function templates have[*corresponding signatures*](#def:signature,corresponding "6.4.1 General [basic.scope.scope]") if their [*template-parameter-list*](temp.pre#nt:template-parameter-list "13.1 Preamble [temp.pre]")*s* have the same length, their corresponding [*template-parameter*](temp.param#nt:template-parameter "13.2 Template parameters [temp.param]")*s* are equivalent, they have equivalent non-object-parameter-type-lists and return types (if any), and, if both are non-static members, they have corresponding object parameters[.](#scope-3.sentence-3) [4](#scope-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1048) Two declarations [*correspond*](#def:correspond "6.4.1 General [basic.scope.scope]") if they (re)introduce the same name, both declare constructors, or both declare destructors, unless - [(4.1)](#scope-4.1) either is a [*using-declarator*](namespace.udecl#nt:using-declarator "9.10 The using declaration [namespace.udecl]"), or - [(4.2)](#scope-4.2) one declares a type (not a type alias) and the other declares a variable, non-static data member other than of an anonymous union ([[class.union.anon]](class.union.anon "11.5.2 Anonymous unions")), enumerator, function, or function template, or - [(4.3)](#scope-4.3) each declares a function or function template and they do not declare corresponding overloads[.](#scope-4.sentence-1) Two function or function template declarations declare[*corresponding overloads*](#def:corresponding_overloads "6.4.1 General [basic.scope.scope]") if - [(4.4)](#scope-4.4) both declare functions with the same non-object-parameter-type-list,[17](#footnote-17 "An implicit object parameter ([over.match.funcs]) is not part of the parameter-type-list.") equivalent ([[temp.over.link]](temp.over.link "13.7.7.2 Function template overloading")) trailing [*requires-clause*](temp.pre#nt:requires-clause "13.1 Preamble [temp.pre]")*s* (if any, except as specified in [[temp.friend]](temp.friend "13.7.5 Friends")), and, if both are non-static members, they have corresponding object parameters, or - [(4.5)](#scope-4.5) both declare function templates with corresponding signatures and equivalent[*template-head*](temp.pre#nt:template-head "13.1 Preamble [temp.pre]")*s* and trailing [*requires-clause*](temp.pre#nt:requires-clause "13.1 Preamble [temp.pre]")*s* (if any)[.](#scope-4.sentence-2) [*Note [2](#scope-note-2)*: Declarations can correspond even if neither binds a name[.](#scope-4.sentence-3) [*Example [1](#scope-example-1)*: struct A {friend void f(); // #1};struct B {friend void f() {} // corresponds to, and defines, #1}; — *end example*] — *end note*] [*Example [2](#scope-example-2)*: typedef int Int;enum E : int { a };void f(int); // #1void f(Int) {} // defines #1void f(E) {} // OK, another overloadstruct X {static void f(); void f() const; // error: redeclarationvoid g(); void g() const; // OKvoid g() &; // error: redeclarationvoid h(this X&, int); void h(int) &&; // OK, another overloadvoid j(this const X&); void j() const &; // error: redeclarationvoid k(); void k(this X&); // error: redeclaration}; — *end example*] [5](#scope-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1124) A declaration is [*name-independent*](#def:declaration,name-independent "6.4.1 General [basic.scope.scope]") if its name is _ (U+005f low line) and it declares - [(5.1)](#scope-5.1) a variable with automatic storage duration, - [(5.2)](#scope-5.2) a structured binding with no [*storage-class-specifier*](dcl.stc#nt:storage-class-specifier "9.2.2 Storage class specifiers [dcl.stc]") and not inhabiting a namespace scope, - [(5.3)](#scope-5.3) a result binding ([[dcl.contract.res]](dcl.contract.res "9.4.2 Referring to the result object")), - [(5.4)](#scope-5.4) the variable introduced by an [*init-capture*](expr.prim.lambda.capture#nt:init-capture "7.5.6.3 Captures [expr.prim.lambda.capture]"), or - [(5.5)](#scope-5.5) a non-static data member of other than an anonymous union[.](#scope-5.sentence-1) *Recommended practice*: Implementations should not emit a warning that a name-independent declaration is used or unused[.](#scope-5.sentence-2) [6](#scope-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1150) Two declarations [*potentially conflict*](#def:potentially_conflict "6.4.1 General [basic.scope.scope]") if they correspond and cause their shared name to denote different entities ([[basic.link]](basic.link "6.7 Program and linkage"))[.](#scope-6.sentence-1) The program is ill-formed if, in any scope, a name is bound to two declarations A and B that potentially conflict and A precedes B ([[basic.lookup]](basic.lookup "6.5 Name lookup")), unless B is name-independent[.](#scope-6.sentence-2) [*Note [3](#scope-note-3)*: An [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") that names a unique name-independent declaration is usable until an additional declaration of the same name is introduced in the same scope ([[basic.lookup.general]](basic.lookup.general "6.5.1 General"))[.](#scope-6.sentence-3) — *end note*] [*Note [4](#scope-note-4)*: Overload resolution can consider potentially conflicting declarations found in multiple scopes (e.g., via [*using-directive*](namespace.udir#nt:using-directive "9.9.4 Using namespace directive [namespace.udir]")*s* or for operator functions), in which case it is often ambiguous[.](#scope-6.sentence-4) — *end note*] [*Example [3](#scope-example-3)*: void f() {int x,y; void x(); // error: different entity for xint y; // error: redefinition}enum { f }; // error: different entity for ​::​fnamespace A {}namespace B = A;namespace B = A; // OK, no effectnamespace B = B; // OK, no effectnamespace A = B; // OK, no effectnamespace B {} // error: different entity for Bvoid g() {int _; _ = 0; // OKint _; // OK, name-independent declaration _ = 0; // error: two non-function declarations in the lookup set}void h () {int _; // #1 _ ++; // OKstatic int _; // error: conflicts with #1 because static variables are not name-independent} — *end example*] [7](#scope-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1199) A declaration is [*nominable*](#def:nominable "6.4.1 General [basic.scope.scope]") in a class, class template, or namespace E at a point P if it precedes P, it does not inhabit a block scope, and its target scope is the scope associated with E or, if E is a namespace, any element of the inline namespace set of E ([[namespace.def]](namespace.def "9.9.2 Namespace definition"))[.](#scope-7.sentence-1) [*Example [4](#scope-example-4)*: namespace A {void f() {void g();}inline namespace B {struct S {friend void h(); static int i; }; }} At the end of this example, the declarations of f, B, S, and h are nominable in A, but those of g and i are not[.](#scope-7.sentence-2) — *end example*] [8](#scope-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1225) When instantiating a templated entity ([[temp.pre]](temp.pre "13.1 Preamble")), any scope S introduced by any part of the template definition is considered to be introduced by the instantiated entity and to contain the instantiations of any declarations that inhabit S[.](#scope-8.sentence-1) [17)](#footnote-17)[17)](#footnoteref-17) An implicit object parameter ([[over.match.funcs]](over.match.funcs "12.2.2 Candidate functions and argument lists")) is not part of the parameter-type-list[.](#footnote-17.sentence-1) ### [6.4.2](#pdecl) Point of declaration [[basic.scope.pdecl]](basic.scope.pdecl) [1](#pdecl-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1237) The [*locus*](#def:locus "6.4.2 Point of declaration [basic.scope.pdecl]") of a declaration ([[basic.pre]](basic.pre "6.1 Preamble")) that is a declarator is immediately after the complete declarator ([[dcl.decl]](dcl.decl "9.3 Declarators"))[.](#pdecl-1.sentence-1) [*Example [1](#pdecl-example-1)*: unsigned char x = 12;{ unsigned char x = x; } Here, the initialization of the second x has undefined behavior, because the initializer accesses the second x outside its lifetime ([[basic.life]](basic.life "6.8.4 Lifetime"))[.](#pdecl-1.sentence-2) — *end example*] [2](#pdecl-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1251) [*Note [1](#pdecl-note-1)*: A name from an outer scope remains visible up to the locus of the declaration that hides it[.](#pdecl-2.sentence-1) [*Example [2](#pdecl-example-2)*: const int i = 2;{ int i[i]; } declares a block-scope array of two integers[.](#pdecl-2.sentence-2) — *end example*] — *end note*] [3](#pdecl-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1265) The locus of a [*class-specifier*](class.pre#nt:class-specifier "11.1 Preamble [class.pre]") is immediately after the [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") or [*simple-template-id*](temp.names#nt:simple-template-id "13.3 Names of template specializations [temp.names]") (if any) in its [*class-head*](class.pre#nt:class-head "11.1 Preamble [class.pre]") ([[class.pre]](class.pre "11.1 Preamble"))[.](#pdecl-3.sentence-1) The locus of an [*enum-specifier*](dcl.enum#nt:enum-specifier "9.8.1 Enumeration declarations [dcl.enum]") is immediately after its [*enum-head*](dcl.enum#nt:enum-head "9.8.1 Enumeration declarations [dcl.enum]"); the locus of an [*opaque-enum-declaration*](dcl.enum#nt:opaque-enum-declaration "9.8.1 Enumeration declarations [dcl.enum]") is immediately after it ([[dcl.enum]](dcl.enum "9.8.1 Enumeration declarations"))[.](#pdecl-3.sentence-2) The locus of an [*alias-declaration*](dcl.pre#nt:alias-declaration "9.1 Preamble [dcl.pre]") is immediately after it[.](#pdecl-3.sentence-3) [4](#pdecl-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1277) The locus of a [*using-declarator*](namespace.udecl#nt:using-declarator "9.10 The using declaration [namespace.udecl]") that does not name a constructor is immediately after the [*using-declarator*](namespace.udecl#nt:using-declarator "9.10 The using declaration [namespace.udecl]") ([[namespace.udecl]](namespace.udecl "9.10 The using declaration"))[.](#pdecl-4.sentence-1) [5](#pdecl-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1282) The locus of an [*enumerator-definition*](dcl.enum#nt:enumerator-definition "9.8.1 Enumeration declarations [dcl.enum]") is immediately after it[.](#pdecl-5.sentence-1) [*Example [3](#pdecl-example-3)*: const int x = 12;{ enum { x = x }; } Here, the enumerator x is initialized with the value of the constant x, namely 12[.](#pdecl-5.sentence-2) — *end example*] [6](#pdecl-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1293) [*Note [2](#pdecl-note-2)*: After the declaration of a class member, the member name can be found in the scope of its class even if the class is an incomplete class[.](#pdecl-6.sentence-1) [*Example [4](#pdecl-example-4)*: struct X {enum E { z = 16 }; int b[X::z]; // OK}; — *end example*] — *end note*] [7](#pdecl-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1309) The locus of an [*elaborated-type-specifier*](dcl.type.elab#nt:elaborated-type-specifier "9.2.9.5 Elaborated type specifiers [dcl.type.elab]") that is a declaration ([[dcl.type.elab]](dcl.type.elab "9.2.9.5 Elaborated type specifiers")) is immediately after it[.](#pdecl-7.sentence-1) [8](#pdecl-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1313) The locus of an injected-class-name declaration ([[class.pre]](class.pre "11.1 Preamble")) is immediately following the opening brace of the class definition[.](#pdecl-8.sentence-1) [9](#pdecl-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1317) The locus of the implicit declaration of a function-local predefined variable ([[dcl.fct.def.general]](dcl.fct.def.general "9.6.1 General")) is immediately before the [*function-body*](dcl.fct.def.general#nt:function-body "9.6.1 General [dcl.fct.def.general]") of its function's definition[.](#pdecl-9.sentence-1) [10](#pdecl-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1323) The locus of the declaration of a structured binding ([[dcl.struct.bind]](dcl.struct.bind "9.7 Structured binding declarations")) is immediately after the [*identifier-list*](cpp.pre#nt:identifier-list "15.1 Preamble [cpp.pre]") of the structured binding declaration[.](#pdecl-10.sentence-1) [11](#pdecl-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1328) The locus of a [*for-range-declaration*](stmt.pre#nt:for-range-declaration "8.1 Preamble [stmt.pre]") of a range-based for statement ([[stmt.ranged]](stmt.ranged "8.6.5 The range-based for statement")) is immediately after the [*for-range-initializer*](stmt.pre#nt:for-range-initializer "8.1 Preamble [stmt.pre]")[.](#pdecl-11.sentence-1) The locus of a [*for-range-declaration*](stmt.pre#nt:for-range-declaration "8.1 Preamble [stmt.pre]") of an expansion statement ([[stmt.expand]](stmt.expand "8.7 Expansion statements")) is immediately after the [*expansion-initializer*](stmt.expand#nt:expansion-initializer "8.7 Expansion statements [stmt.expand]")[.](#pdecl-11.sentence-2) [12](#pdecl-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1336) The locus of a [*template-parameter*](temp.param#nt:template-parameter "13.2 Template parameters [temp.param]") is immediately after it[.](#pdecl-12.sentence-1) [*Example [5](#pdecl-example-5)*: typedef unsigned char T;template struct A { }; — *end example*] [13](#pdecl-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1348) The locus of a [*result-name-introducer*](dcl.contract.res#nt:result-name-introducer "9.4.2 Referring to the result object [dcl.contract.res]") ([[dcl.contract.res]](dcl.contract.res "9.4.2 Referring to the result object")) is immediately after it[.](#pdecl-13.sentence-1) [14](#pdecl-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1352) The locus of a [*concept-definition*](temp.concept#nt:concept-definition "13.7.9 Concept definitions [temp.concept]") is immediately after its [*concept-name*](temp.concept#nt:concept-name "13.7.9 Concept definitions [temp.concept]") ([[temp.concept]](temp.concept "13.7.9 Concept definitions"))[.](#pdecl-14.sentence-1) [*Note [3](#pdecl-note-3)*: The [*constraint-expression*](temp.constr.decl#nt:constraint-expression "13.5.3 Constrained declarations [temp.constr.decl]") cannot use the [*concept-name*](temp.concept#nt:concept-name "13.7.9 Concept definitions [temp.concept]")[.](#pdecl-14.sentence-2) — *end note*] [15](#pdecl-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1360) The locus of a [*namespace-definition*](namespace.def.general#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") with an [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") is immediately after the [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]")[.](#pdecl-15.sentence-1) [*Note [4](#pdecl-note-4)*: An identifier is invented for an [*unnamed-namespace-definition*](namespace.def.general#nt:unnamed-namespace-definition "9.9.2.1 General [namespace.def.general]") ([[namespace.unnamed]](namespace.unnamed "9.9.2.2 Unnamed namespaces"))[.](#pdecl-15.sentence-2) — *end note*] [16](#pdecl-16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1369) [*Note [5](#pdecl-note-5)*: Friend declarations can introduce functions or classes that belong to the nearest enclosing namespace or block scope, but they do not bind names anywhere ([[class.friend]](class.friend "11.8.4 Friends"))[.](#pdecl-16.sentence-1) Function declarations at block scope and variable declarations with the extern specifier at block scope declare entities that belong to the nearest enclosing namespace, but they do not bind names in it[.](#pdecl-16.sentence-2) — *end note*] [17](#pdecl-17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1381) [*Note [6](#pdecl-note-6)*: For point of instantiation of a template, see [[temp.point]](temp.point "13.8.4.1 Point of instantiation")[.](#pdecl-17.sentence-1) — *end note*] ### [6.4.3](#block) Block scope [[basic.scope.block]](basic.scope.block) [1](#block-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1391) Each - [(1.1)](#block-1.1) selection, iteration, or expansion statement ([[stmt.select]](stmt.select "8.5 Selection statements"), [[stmt.iter]](stmt.iter "8.6 Iteration statements"), [[stmt.expand]](stmt.expand "8.7 Expansion statements")), - [(1.2)](#block-1.2) substatement of such a statement, - [(1.3)](#block-1.3) [*handler*](except.pre#nt:handler "14.1 Preamble [except.pre]") ([[except.pre]](except.pre "14.1 Preamble")), or - [(1.4)](#block-1.4) compound statement ([[stmt.block]](stmt.block "8.4 Compound statement or block")) that is not the [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") of a [*handler*](except.pre#nt:handler "14.1 Preamble [except.pre]") introduces a [*block scope*](#def:scope,block "6.4.3 Block scope [basic.scope.block]") that includes that statement or [*handler*](except.pre#nt:handler "14.1 Preamble [except.pre]")[.](#block-1.sentence-1) [*Note [1](#block-note-1)*: A substatement that is also a block has only one scope[.](#block-1.sentence-2) — *end note*] A variable that belongs to a block scope is a [*block variable*](#def:block_variable "6.4.3 Block scope [basic.scope.block]")[.](#block-1.sentence-3) [*Example [1](#block-example-1)*: int i = 42;int a[10]; for (int i = 0; i < 10; i++) a[i] = i; int j = i; // j = 42 — *end example*] [2](#block-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1423) If a declaration that is not a name-independent declaration and that binds a name in the block scope S of a - [(2.1)](#block-2.1) [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") of a [*lambda-expression*](expr.prim.lambda.general#nt:lambda-expression "7.5.6.1 General [expr.prim.lambda.general]"),[*function-body*](dcl.fct.def.general#nt:function-body "9.6.1 General [dcl.fct.def.general]"), or [*function-try-block*](except.pre#nt:function-try-block "14.1 Preamble [except.pre]"), - [(2.2)](#block-2.2) substatement of a selection or iteration statement that is not itself a selection or iteration statement, or - [(2.3)](#block-2.3) [*handler*](except.pre#nt:handler "14.1 Preamble [except.pre]") of a [*function-try-block*](except.pre#nt:function-try-block "14.1 Preamble [except.pre]") potentially conflicts with a declaration whose target scope is the parent scope of S, the program is ill-formed[.](#block-2.sentence-1) [*Example [2](#block-example-2)*: if (int x = f()) {int x; // error: redeclaration of x}else {int x; // error: redeclaration of x} — *end example*] ### [6.4.4](#param) Function parameter scope [[basic.scope.param]](basic.scope.param) [1](#param-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1456) A [*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6 Functions [dcl.fct]") P introduces a [*function parameter scope*](#def:scope,function_parameter "6.4.4 Function parameter scope [basic.scope.param]") that includes P[.](#param-1.sentence-1) [*Note [1](#param-note-1)*: A function parameter cannot be used for its value within the [*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6 Functions [dcl.fct]") ([[dcl.fct.default]](dcl.fct.default "9.3.4.7 Default arguments"))[.](#param-1.sentence-2) — *end note*] - [(1.1)](#param-1.1) If P is associated with a [*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]") and is preceded by a (possibly-parenthesized) [*noptr-declarator*](dcl.decl.general#nt:noptr-declarator "9.3.1 General [dcl.decl.general]") of the form[*declarator-id*](dcl.decl.general#nt:declarator-id "9.3.1 General [dcl.decl.general]") [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")opt, its scope extends to the end of the nearest enclosing[*init-declarator*](dcl.decl.general#nt:init-declarator "9.3.1 General [dcl.decl.general]"),[*member-declarator*](class.mem.general#nt:member-declarator "11.4.1 General [class.mem.general]"),[*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]") of a [*parameter-declaration*](dcl.fct#nt:parameter-declaration "9.3.4.6 Functions [dcl.fct]") or a [*nodeclspec-function-declaration*](dcl.pre#nt:nodeclspec-function-declaration "9.1 Preamble [dcl.pre]"), or[*function-definition*](dcl.fct.def.general#nt:function-definition "9.6.1 General [dcl.fct.def.general]"), but does not include the locus of the associated [*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]")[.](#param-1.1.sentence-1) [*Note [2](#param-note-2)*: In this case, P declares the parameters of a function (or a function or template parameter declared with function type)[.](#param-1.1.sentence-2) A member function's parameter scope is nested within its class's scope[.](#param-1.1.sentence-3) — *end note*] - [(1.2)](#param-1.2) If P is associated with a [*lambda-declarator*](expr.prim.lambda.general#nt:lambda-declarator "7.5.6.1 General [expr.prim.lambda.general]"), its scope extends to the end of the [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") in the [*lambda-expression*](expr.prim.lambda.general#nt:lambda-expression "7.5.6.1 General [expr.prim.lambda.general]")[.](#param-1.2.sentence-1) - [(1.3)](#param-1.3) If P is associated with a [*requirement-parameter-list*](expr.prim.req.general#nt:requirement-parameter-list "7.5.8.1 General [expr.prim.req.general]"), its scope extends to the end of the [*requirement-body*](expr.prim.req.general#nt:requirement-body "7.5.8.1 General [expr.prim.req.general]") of the [*requires-expression*](expr.prim.req.general#nt:requires-expression "7.5.8.1 General [expr.prim.req.general]")[.](#param-1.3.sentence-1) - [(1.4)](#param-1.4) If P is associated with a [*deduction-guide*](temp.deduct.guide#nt:deduction-guide "13.7.2.3 Deduction guides [temp.deduct.guide]"), its scope extends to the end of the [*deduction-guide*](temp.deduct.guide#nt:deduction-guide "13.7.2.3 Deduction guides [temp.deduct.guide]")[.](#param-1.4.sentence-1) ### [6.4.5](#lambda) Lambda scope [[basic.scope.lambda]](basic.scope.lambda) A [*lambda-expression*](expr.prim.lambda.general#nt:lambda-expression "7.5.6.1 General [expr.prim.lambda.general]") E introduces a [*lambda scope*](#def:scope,lambda "6.4.5 Lambda scope [basic.scope.lambda]") that starts immediately after the [*lambda-introducer*](expr.prim.lambda.general#nt:lambda-introducer "7.5.6.1 General [expr.prim.lambda.general]") of E and extends to the end of the [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") of E[.](#lambda-sentence-1) ### [6.4.6](#namespace) Namespace scope [[basic.scope.namespace]](basic.scope.namespace) [1](#namespace-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1501) Any [*namespace-definition*](namespace.def.general#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") for a namespace N introduces a [*namespace scope*](#def:scope,namespace "6.4.6 Namespace scope [basic.scope.namespace]") that includes the [*namespace-body*](namespace.def.general#nt:namespace-body "9.9.2.1 General [namespace.def.general]") for every [*namespace-definition*](namespace.def.general#nt:namespace-definition "9.9.2.1 General [namespace.def.general]") for N[.](#namespace-1.sentence-1) For each non-friend redeclaration or specialization whose target scope is or is contained by the scope, the portion after the[*declarator-id*](dcl.decl.general#nt:declarator-id "9.3.1 General [dcl.decl.general]"),[*class-head-name*](class.pre#nt:class-head-name "11.1 Preamble [class.pre]"), or[*enum-head-name*](dcl.enum#nt:enum-head-name "9.8.1 Enumeration declarations [dcl.enum]") is also included in the scope[.](#namespace-1.sentence-2) The global scope is the namespace scope of the global namespace ([[basic.namespace]](basic.namespace "9.9 Namespaces"))[.](#namespace-1.sentence-3) [*Example [1](#namespace-example-1)*: namespace Q {namespace V { void f(); }void V::f() { // in the scope of Vvoid h(); // declares Q​::​V​::​h}} — *end example*] ### [6.4.7](#class) Class scope [[basic.scope.class]](basic.scope.class) [1](#class-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1528) Any declaration of a class or class template C introduces a [*class scope*](#def:scope,class "6.4.7 Class scope [basic.scope.class]") that includes the [*member-specification*](class.mem.general#nt:member-specification "11.4.1 General [class.mem.general]") of the [*class-specifier*](class.pre#nt:class-specifier "11.1 Preamble [class.pre]") for C (if any)[.](#class-1.sentence-1) For each non-friend redeclaration or specialization whose target scope is or is contained by the scope, the portion after the[*declarator-id*](dcl.decl.general#nt:declarator-id "9.3.1 General [dcl.decl.general]"),[*class-head-name*](class.pre#nt:class-head-name "11.1 Preamble [class.pre]"), or[*enum-head-name*](dcl.enum#nt:enum-head-name "9.8.1 Enumeration declarations [dcl.enum]") is also included in the scope[.](#class-1.sentence-2) [*Note [1](#class-note-1)*: Lookup from a program point before the [*class-specifier*](class.pre#nt:class-specifier "11.1 Preamble [class.pre]") of a class will find no bindings in the class scope[.](#class-1.sentence-3) [*Example [1](#class-example-1)*: templatestruct B { D::type x; // #1}; struct A { using type = int; };struct C : A, B {}; // error at #1: C​::​type not found — *end example*] — *end note*] ### [6.4.8](#enum) Enumeration scope [[basic.scope.enum]](basic.scope.enum) [1](#enum-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1558) Any declaration of an enumeration E introduces an [*enumeration scope*](#def:scope,enumeration "6.4.8 Enumeration scope [basic.scope.enum]") that includes the [*enumerator-list*](dcl.enum#nt:enumerator-list "9.8.1 Enumeration declarations [dcl.enum]") of the [*enum-specifier*](dcl.enum#nt:enum-specifier "9.8.1 Enumeration declarations [dcl.enum]") for E (if any)[.](#enum-1.sentence-1) ### [6.4.9](#temp) Template parameter scope [[basic.scope.temp]](basic.scope.temp) [1](#temp-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1566) Each[*type-tt-parameter*](temp.param#nt:type-tt-parameter "13.2 Template parameters [temp.param]"),[*variable-tt-parameter*](temp.param#nt:variable-tt-parameter "13.2 Template parameters [temp.param]"), and[*concept-tt-parameter*](temp.param#nt:concept-tt-parameter "13.2 Template parameters [temp.param]") introduces a [*template parameter scope*](#def:scope,template_parameter "6.4.9 Template parameter scope [basic.scope.temp]") that includes the [*template-head*](temp.pre#nt:template-head "13.1 Preamble [temp.pre]") of the [*template-parameter*](temp.param#nt:template-parameter "13.2 Template parameters [temp.param]")[.](#temp-1.sentence-1) [2](#temp-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1576) Each [*template-declaration*](temp.pre#nt:template-declaration "13.1 Preamble [temp.pre]") D introduces a template parameter scope that extends from the beginning of its [*template-parameter-list*](temp.pre#nt:template-parameter-list "13.1 Preamble [temp.pre]") to the end of the [*template-declaration*](temp.pre#nt:template-declaration "13.1 Preamble [temp.pre]")[.](#temp-2.sentence-1) Any declaration outside the [*template-parameter-list*](temp.pre#nt:template-parameter-list "13.1 Preamble [temp.pre]") that would inhabit that scope instead inhabits the same scope as D[.](#temp-2.sentence-2) The parent scope of any scope S that is not a template parameter scope is the smallest scope that contains S and is not a template parameter scope[.](#temp-2.sentence-3) [*Note [1](#temp-note-1)*: Therefore, only template parameters belong to a template parameter scope, and only template parameter scopes have a template parameter scope as a parent scope[.](#temp-2.sentence-4) — *end note*] ### [6.4.10](#contract) Contract-assertion scope [[basic.scope.contract]](basic.scope.contract) [1](#contract-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1593) Each contract assertion ([[basic.contract]](basic.contract "6.11 Contract assertions"))C introduces a [*contract-assertion scope*](#def:scope,contract-assertion "6.4.10 Contract-assertion scope [basic.scope.contract]") that includes C[.](#contract-1.sentence-1) [2](#contract-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1598) If a [*result-name-introducer*](dcl.contract.res#nt:result-name-introducer "9.4.2 Referring to the result object [dcl.contract.res]") ([[dcl.contract.res]](dcl.contract.res "9.4.2 Referring to the result object")) that is not name-independent ([[basic.scope.scope]](#scope "6.4.1 General")) and whose enclosing postcondition assertion is associated with a function F potentially conflicts with a declaration whose target scope is - [(2.1)](#contract-2.1) the function parameter scope of F or - [(2.2)](#contract-2.2) if associated with a [*lambda-declarator*](expr.prim.lambda.general#nt:lambda-declarator "7.5.6.1 General [expr.prim.lambda.general]"), the nearest enclosing lambda scope of the precondition assertion ([[expr.prim.lambda]](expr.prim.lambda "7.5.6 Lambda expressions")), the program is ill-formed[.](#contract-2.sentence-1)