Стандарт C++ 11 (1119564), страница 16
Текст из файла (страница 16)
In general, each particular name is valid only within some possibly discontiguousportion of program text called its scope. To determine the scope of a declaration, it is sometimes convenientto refer to the potential scope of a declaration. The scope of a declaration is the same as its potential scopeunless the potential scope contains another declaration of the same name. In that case, the potential scope27) 8.3.6 describes how default argument names are looked up.§ 3.3.138© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)of the declaration in the inner (contained) declarative region is excluded from the scope of the declarationin the outer (containing) declarative region.2[ Example: inint j = 24;int main() {int i = j, j;j = 42;}the identifier j is declared twice as a name (and used twice).
The declarative region of the first j includesthe entire example. The potential scope of the first j begins immediately after that j and extends to theend of the program, but its (actual) scope excludes the text between the , and the }. The declarative regionof the second declaration of j (the j immediately before the semicolon) includes all the text between { and}, but its potential scope excludes the declaration of i. The scope of the second declaration of j is the sameas its potential scope.
— end example ]3The names declared by a declaration are introduced into the scope in which the declaration occurs, exceptthat the presence of a friend specifier (11.3), certain uses of the elaborated-type-specifier (7.1.6.3), andusing-directives (7.3.4) alter this general behavior.4Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,— they shall all refer to the same entity, or all refer to functions and function templates; or— exactly one declaration shall declare a class name or enumeration name that is not a typedef nameand the other declarations shall all refer to the same variable or enumerator, or all refer to functionsand function templates; in this case the class name or enumeration name is hidden (3.3.10). [ Note: Anamespace name or a class template name must be unique in its declarative region (7.3.2, Clause 14).— end note ][ Note: These restrictions apply to the declarative region into which a name is introduced, which is not necessarily the same as the region in which the declaration occurs.
In particular, elaborated-type-specifiers (7.1.6.3)and friend declarations (11.3) may introduce a (possibly not visible) name into an enclosing namespace; theserestrictions apply to that region. Local extern declarations (3.5) may introduce a name into the declarativeregion where the declaration appears and also introduce a (possibly not visible) name into an enclosingnamespace; these restrictions apply to both regions. — end note ]5[ Note: The name lookup rules are summarized in 3.4. — end note ]3.3.21Point of declaration[basic.scope.pdecl]The point of declaration for a name is immediately after its complete declarator (Clause 8) and before itsinitializer (if any), except as noted below.
[ Example:int x = 12;{ int x = x; }Here the second x is initialized with its own (indeterminate) value. — end example ]2[ Note: a name from an outer scope remains visible up to the point of declaration of the name that hidesit.[ Example:const int i = 2;{ int i[i]; }§ 3.3.2© ISO/IEC 2011 – All rights reserved39ISO/IEC 14882:2011(E)declares a block-scope array of two integers.
— end example ] — end note ]3The point of declaration for a class or class template first declared by a class-specifier is immediatelyafter the identifier or simple-template-id (if any) in its class-head (Clause 9). The point of declarationfor an enumeration is immediately after the identifier (if any) in either its enum-specifier (7.2) or its firstopaque-enum-declaration (7.2), whichever comes first. The point of declaration of an alias or alias templateimmediately follows the type-id to which the alias refers.4The point of declaration for an enumerator is immediately after its enumerator-definition.[ Example:const int x = 12;{ enum { x = x }; }Here, the enumerator x is initialized with the value of the constant x, namely 12.
— end example ]5After the point of declaration of a class member, the member name can be looked up in the scope of itsclass. [ Note: this is true even if the class is an incomplete class. For example,struct X {enum E { z = 16 };int b[X::z];// OK};— end note ]6The point of declaration of a class first declared in an elaborated-type-specifier is as follows:— for a declaration of the formclass-key attribute-specifier-seqopt identifier ;the identifier is declared to be a class-name in the scope that contains the declaration, otherwise— for an elaborated-type-specifier of the formclass-key identifierif the elaborated-type-specifier is used in the decl-specifier-seq or parameter-declaration-clause of afunction defined in namespace scope, the identifier is declared as a class-name in the namespace thatcontains the declaration; otherwise, except as a friend declaration, the identifier is declared in thesmallest namespace or block scope that contains the declaration.
[ Note: These rules also apply withintemplates. — end note ] [ Note: Other forms of elaborated-type-specifier do not declare a new name,and therefore must refer to an existing type-name. See 3.4.4 and 7.1.6.3. — end note ]7The point of declaration for an injected-class-name (Clause 9) is immediately following the opening braceof the class definition.8The point of declaration for a function-local predefined variable (8.4) is immediately before the function-bodyof a function definition.9The point of declaration for a template parameter is immediately after its complete template-parameter.[ Example:typedef unsigned char T;template<class T= T// lookup finds the typedef name of unsigned char, T// lookup finds the template parameterN = 0> struct A { };§ 3.3.240© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— end example ]10[ Note: Friend declarations refer to functions or classes that are members of the nearest enclosing namespace,but they do not introduce new names into that namespace (7.3.1.2).
Function declarations at block scopeand variable declarations with the extern specifier at block scope refer to declarations that are members ofan enclosing namespace, but they do not introduce new names into that scope. — end note ]11[ Note: For point of instantiation of a template, see 14.6.4.1. — end note ]3.3.3Block scope[basic.scope.local]1A name declared in a block (6.3) is local to that block; it has block scope.
Its potential scope begins at itspoint of declaration (3.3.2) and ends at the end of its block. A variable declared at block scope is a localvariable.2The potential scope of a function parameter name (including one appearing in a lambda-declarator) or ofa function-local predefined variable in a function definition (8.4) begins at its point of declaration. If thefunction has a function-try-block the potential scope of a parameter or of a function-local predefined variableends at the end of the last associated handler, otherwise it ends at the end of the outermost block ofthe function definition. A parameter name shall not be redeclared in the outermost block of the functiondefinition nor in the outermost block of any handler associated with a function-try-block.3The name declared in an exception-declaration is local to the handler and shall not be redeclared in theoutermost block of the handler.4Names declared in the for-init-statement, the for-range-declaration, and in the condition of if, while, for,and switch statements are local to the if, while, for, or switch statement (including the controlledstatement), and shall not be redeclared in a subsequent condition of that statement nor in the outermostblock (or, for the if statement, any of the outermost blocks) of the controlled statement; see 6.4.3.3.41Function scope[basic.funscope]Labels (6.1) have function scope and may be used anywhere in the function in which they are declared.
Onlylabels have function scope.3.3.61[basic.scope.proto]In a function declaration, or in any function declarator except the declarator of a function definition (8.4),names of parameters (if supplied) have function prototype scope, which terminates at the end of the nearestenclosing function declarator.3.3.51Function prototype scopeNamespace scope[basic.scope.namespace]The declarative region of a namespace-definition is its namespace-body. The potential scope denoted byan original-namespace-name is the concatenation of the declarative regions established by each of thenamespace-definitions in the same declarative region with that original-namespace-name. Entities declaredin a namespace-body are said to be members of the namespace, and names introduced by these declarationsinto the declarative region of the namespace are said to be member names of the namespace.
A namespacemember name has namespace scope. Its potential scope includes its namespace from the name’s point ofdeclaration (3.3.2) onwards; and for each using-directive (7.3.4) that nominates the member’s namespace,the member’s potential scope includes that portion of the potential scope of the using-directive that followsthe member’s point of declaration. [ Example:namespace N {int i;§ 3.3.6© ISO/IEC 2011 – All rights reserved41ISO/IEC 14882:2011(E)int g(int a) { return a; }int j();void q();}namespace { int l=1; }// the potential scope of l is from its point of declaration// to the end of the translation unitnamespace N {int g(char a) {return l+a;}// overloads N::g(int)// l is from unnamed namespaceint i;int j();// error: duplicate definition// OK: duplicate function declarationint j() {return g(i);}int q();// OK: definition of N::j()// calls N::g(int)// error: different return type}— end example ]2A namespace member can also be referred to after the :: scope resolution operator (5.1) applied to the nameof its namespace or the name of a namespace which nominates the member’s namespace in a using-directive;see 3.4.3.2.3The outermost declarative region of a translation unit is also a namespace, called the global namespace.
Aname declared in the global namespace has global namespace scope (also called global scope). The potentialscope of such a name begins at its point of declaration (3.3.2) and ends at the end of the translation unitthat is its declarative region. Names with global namespace scope are said to be global name.3.3.71Class scope[basic.scope.class]The following rules describe the scope of names declared in classes.1) The potential scope of a name declared in a class consists not only of the declarative region followingthe name’s point of declaration, but also of all function bodies, brace-or-equal-initializers of non-staticdata members, and default arguments in that class (including such things in nested classes).2) A name N used in a class S shall refer to the same declaration in its context and when re-evaluated inthe completed scope of S.