[basic.lookup.unqual] (original) (raw)
6 Basics [basic]
6.5 Name lookup [basic.lookup]
6.5.1 Unqualified name lookup [basic.lookup.unqual]
In all the cases listed in [basic.lookup.unqual], the scopes are searched for a declaration in the order listed in each of the respective categories; name lookup ends as soon as a declaration is found for the name.
If no declaration is found, the program is ill-formed.
For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace.
[ Note
:
For purposes of determining (during parsing) whether an expression is apostfix-expression for a function call, the usual name lookup rules apply.
For example,
int h; void g(); namespace N { struct A {}; template int f(T); template int g(T); template int h(T); }
int x = f<N::A>(N::A());
int y = g<N::A>(N::A());
int z = h<N::A>(N::A());
The rules in [basic.lookup.argdep] have no effect on the syntactic interpretation of an expression.
For example,
typedef int f;
namespace N {
struct A {
friend void f(A &);
operator int();
void g(A a) {
int i = f(a);
}
};
}
Because the expression is not a function call, the argument-dependent name lookup ([basic.lookup.argdep]) does not apply and the friend function f is not found.
— end note
]
A name used in global scope, outside of any function, class or user-declared namespace, shall be declared before its use in global scope.
A name used in a user-declared namespace outside of the definition of any function or class shall be declared before its use in that namespace or before its use in a namespace enclosing its namespace.
In the definition of a function that is a member of namespace N, a name used after the function'sdeclarator-id21shall be declared before its use in the block in which it is used or in one of its enclosing blocks ([stmt.block]) or shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N's enclosing namespaces.
[ Example
:
namespace A { namespace N { void f(); } } void A::N::f() { i = 5;
}
— end example
]
A name used in the definition of a class X22outside of a complete-class context ([class.mem]) of Xshall be declared in one of the following ways:
- before its use in class X or be a member of a base class of X ([class.member.lookup]), or
- if X is a nested class of classY, before the definition of X inY, 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),23or
- if X is a local class or is a nested class of a local class, before the definition of class X in a block enclosing 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 class or a nested class within a local class of a function that is a member ofN, before the definition of class X in namespaceN or in one of N's enclosing namespaces.
[ Example
:
namespace M { class B { }; }
namespace N { class Y : public M::B { class X { int a[i]; }; }; }
— end example
]
[ Note
:
When looking for a prior declaration of a class or function introduced by a friend declaration, scopes outside of the innermost enclosing namespace scope are not considered; see [namespace.memdef].
— end note
]
[ Note
:
[basic.scope.class]further describes the restrictions on the use of names in a class definition.
[class.nest] further describes the restrictions on the use of names in nested class definitions.
[class.local] further describes the restrictions on the use of names in local class definitions.
— end note
]
For the members of a class X, a name used in a complete-class context ([class.mem]) of X or in the definition of a class member outside of the definition of X, following the member'sdeclarator-id24, shall be declared in one of the following ways:
- before its use in the block in which it is used or in an enclosing block ([stmt.block]), or
- shall be a member of class X or be a member of a base class of X ([class.member.lookup]), or
- if Xis a nested class of class Y, 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),25or
- if X is a local class or is a nested class of a local class, before the definition of class X in a block enclosing 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 class or a nested class within a local class of a function that is a member ofN, before the use of the name, in namespace Nor 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; }
— end example
]
[ Note
:
[class.mfct] and [class.static] further describe the restrictions on the use of names in member function definitions.
[class.nest] further describes the restrictions on the use of names in the scope of nested classes.
[class.local] further describes the restrictions on the use of names in local class definitions.
— end note
]
Name lookup for a name used in the definition of a friend function defined inline in the class granting friendship shall proceed as described for lookup in member function definitions.
If the friend function is not defined in the class granting friendship, name lookup in the friend function definition shall proceed as described for lookup in namespace member function definitions.
In a friend declaration naming a member function, a name used in the function declarator and not part of a template-argumentin the declarator-id is first looked up in the scope of the member function's class ([class.member.lookup]).
If it is not found, or if the name is part of atemplate-argument in the declarator-id, the look up is as described for unqualified names in the definition of the class granting friendship.
[ Example
:
struct A {
typedef int AT;
void f1(AT);
void f2(float);
template void f3();
};
struct B {
typedef char AT;
typedef float BT;
friend void A::f1(AT);
friend void A::f2(BT);
friend void A::f3();
};
— end example
]
[ Note
:
[dcl.fct.default] further describes the restrictions on the use of names in default arguments.
— end note
]
A name used in the definition of a static data memberof class X (after the qualified-idof the static member) is looked up as if the name was used in a member function of X.
[ Note
:
[class.static.data] further describes the restrictions on the use of names in the definition of astatic data member.
— end note
]
If a variable member of a namespace is defined outside of the scope of its namespace then any name that appears in the definition of the member (after the declarator-id) is looked up as if the definition of the member occurred in its namespace.
[ Example
:
namespace N { int i = 4; extern int j; }
int i = 2;
int N::j = i;
— end example
]
A name used in the handler for a function-try-blockis looked up as if the name was used in the outermost block of the function definition.
In particular, the function parameter names shall not be redeclared in theexception-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 scope of a handler for the function-try-block.
[ Note
:
But function parameter names are found.
— end note
]
[ Note
:
The rules for name lookup in template definitions are described in [temp.res].
— end note
]