Стандарт C++ 11 (1119564), страница 18
Текст из файла (страница 18)
—end note ] [ Note: 3.3.7 further describes the restrictions on the use of names in a class definition. 9.7 furtherdescribes the restrictions on the use of names in nested class definitions. 9.8 further describes the restrictionson the use of names in local class definitions. — end note ]8A name used in the definition of a member function (9.3) of class X following the function’s declarator-id 31or in the brace-or-equal-initializer of a non-static data member (9.2) of class X shall be declared in one ofthe following ways:— before its use in the block in which it is used or in an enclosing block (6.3), or— shall be a member of class X or be a member of a base class of X (10.2), or— if X is a nested class of class Y (9.7), shall be a member of Y, or shall be a member of a base class of Y(this lookup applies in turn to Y’s enclosing classes, starting with the innermost enclosing class),32 or— if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a blockenclosing the definition of class X, or— if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local classor a nested class within a local class of a function that is a member of N, before the use of the name,in namespace N or in one of N ’s enclosing namespaces.[ Example:class B { };namespace M {namespace N {class X : public B {void f();};}}void M::N::X::f() {i = 16;}31) That is, an unqualified name that occurs, for instance, in a type or default argument in the parameter-declaration-clauseor in the function body.32) This lookup applies whether the member function is defined within the definition of class X or whether the member functionis defined in a namespace scope enclosing X’s definition.§ 3.4.1© ISO/IEC 2011 – All rights reserved47ISO/IEC 14882:2011(E)//////////////The following scopes are searched for a declaration of i:1) outermost block scope of M::N::X::f, before the use of i2) scope of class M::N::X3) scope of M::N::X’s base class B4) scope of namespace M::N5) scope of namespace M6) global scope, before the definition of M::N::X::f— end example ] [ Note: 9.3 and 9.4 further describe the restrictions on the use of names in member functiondefinitions.
9.7 further describes the restrictions on the use of names in the scope of nested classes. 9.8further describes the restrictions on the use of names in local class definitions. — end note ]9Name lookup for a name used in the definition of a friend function (11.3) defined inline in the class grantingfriendship shall proceed as described for lookup in member function definitions.
If the friend function isnot defined in the class granting friendship, name lookup in the friend function definition shall proceed asdescribed for lookup in namespace member function definitions.10In a friend declaration naming a member function, a name used in the function declarator and not part of atemplate-argument in the declarator-id is first looked up in the scope of the member function’s class (10.2). Ifit is not found, or if the name is part of a template-argument in the declarator-id, the look up is as describedfor unqualified names in the definition of the class granting friendship. [ Example:struct A {typedef int AT;void f1(AT);void f2(float);template <class T> void f3();};struct B {typedef char AT;typedef float BT;friend void A::f1(AT);// parameter type is A::ATfriend void A::f2(BT);// parameter type is B::BTfriend void A::f3<AT>();// template argument is B::AT};— end example ]11During the lookup for a name used as a default argument (8.3.6) in a function parameter-declaration-clauseor used in the expression of a mem-initializer for a constructor (12.6.2), the function parameter names arevisible and hide the names of entities declared in the block, class or namespace scopes containing the functiondeclaration.
[ Note: 8.3.6 further describes the restrictions on the use of names in default arguments. 12.6.2further describes the restrictions on the use of names in a ctor-initializer. — end note ]12During the lookup of a name used in the constant-expression of an enumerator-definition, previously declaredenumerators of the enumeration are visible and hide the names of entities declared in the block, class, ornamespace scopes containing the enum-specifier.13A name used in the definition of a static data member of class X (9.4.2) (after the qualified-id of the staticmember) is looked up as if the name was used in a member function of X.
[ Note: 9.4.2 further describes therestrictions on the use of names in the definition of a static data member. — end note ]14If a variable member of a namespace is defined outside of the scope of its namespace then any name thatappears in the definition of the member (after the declarator-id) is looked up as if the definition of themember occurred in its namespace. [ Example:§ 3.4.148© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)namespace N {int i = 4;extern int j;}int i = 2;int N::j = i;// N::j == 4— end example ]15A name used in the handler for a function-try-block (Clause 15) is looked up as if the name was used inthe outermost block of the function definition. In particular, the function parameter names shall not beredeclared in the exception-declaration nor in the outermost block of a handler for the function-try-block.Names declared in the outermost block of the function definition are not found when looked up in the scopeof a handler for the function-try-block.
[ Note: But function parameter names are found. — end note ]16[ Note: The rules for name lookup in template definitions are described in 14.6. — end note ]3.4.21Argument-dependent name lookup[basic.lookup.argdep]When the postfix-expression in a function call (5.2.2) is an unqualified-id, other namespaces not consideredduring the usual unqualified lookup (3.4.1) may be searched, and in those namespaces, namespace-scopefriend function declarations (11.3) not otherwise visible may be found.
These modifications to the searchdepend on the types of the arguments (and for template template arguments, the namespace of the templateargument). [ Example:namespace N {struct S { };void f(S);}void g() {N::S s;f(s);(f)(s);// OK: calls N::f// error: N::f not considered; parentheses// prevent argument-dependent lookup}— end example ]2For each argument type T in the function call, there is a set of zero or more associated namespaces and aset of zero or more associated classes to be considered.
The sets of namespaces and classes is determinedentirely by the types of the function arguments (and the namespace of any template template argument).Typedef names and using-declarations used to specify the types do not contribute to this set. The sets ofnamespaces and classes are determined in the following way:— If T is a fundamental type, its associated sets of namespaces and classes are both empty.— If T is a class type (including unions), its associated classes are: the class itself; the class of which it is amember, if any; and its direct and indirect base classes.
Its associated namespaces are the namespacesof which its associated classes are members. Furthermore, if T is a class template specialization,its associated namespaces and classes also include: the namespaces and classes associated with thetypes of the template arguments provided for template type parameters (excluding template templateparameters); the namespaces of which any template template arguments are members; and the classes§ 3.4.2© ISO/IEC 2011 – All rights reserved49ISO/IEC 14882:2011(E)of which any member templates used as template template arguments are members.
[ Note: Non-typetemplate arguments do not contribute to the set of associated namespaces. — end note ]— If T is an enumeration type, its associated namespace is the namespace in which it is defined. If it isclass member, its associated class is the member’s class; else it has no associated class.— If T is a pointer to U or an array of U, its associated namespaces and classes are those associated withU.— If T is a function type, its associated namespaces and classes are those associated with the functionparameter types and those associated with the return type.— If T is a pointer to a member function of a class X, its associated namespaces and classes are thoseassociated with the function parameter types and return type, together with those associated with X.— If T is a pointer to a data member of class X, its associated namespaces and classes are those associatedwith the member type together with those associated with X.If an associated namespace is an inline namespace (7.3.1), its enclosing namespace is also included in the set.If an associated namespace directly contains inline namespaces, those inline namespaces are also included inthe set.