Well, in broad strokes:
-
If the name is preceded by
::
, as in::A
orX::A
, then use qualified name lookup. First look upX
, if it exists (if not use the global namespace) then look inside it forA
. IfX
is a class, andA
is not a direct member, then look in all the direct bases ofX
. IfA
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 forA
in the namespace the type ofX
was declared in, in the friends ofX
, and ifX
is a template instantiation, likewise for all the arguments involved. Scopes associated only bytypedef
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.