Well, in broad strokes:
If the name is preceded by ::
, as in ::A
or X::A
, then use qualified name lookup. First look up X
, if it exists (if not use the global namespace) then look inside it for A
. If X
is a class, and A
is not a direct member, then look in all the direct bases of X
. If A
is found in more than one base, fail.
Otherwise, if the name is used as a function call such as A( X )
, use argument-dependent lookup. This is the hard part. Look for A
in the namespace the type of X
was declared in, in the friends of X
, and if X
is a template instantiation, likewise for all the arguments involved. Scopes associated only by typedef
do not apply. Do this in addition to unqualified lookup.
Start with unqualified lookup if argument-dependent lookup doesn't apply. This is the usual way variables are found. Start at the current scope and work outwards until the name is found. Note that this respects using namespace
directives, which the other two cases do not.
Simply glancing at the Standard will reveal many exceptions and gotchas. For example, unqualified lookup is used to determine whether the name is used as a function call, as opposed to a cast-expression, before ADL is used to generate a list of potential overloads. Unqualified lookup doesn't look for objects in enclosing scopes of nested of local classes, because such objects might not exist at the time of reference.
Apply common sense, and ask more specific questions when (as often it does) intuition fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With