Well, in broad strokes:
-
If the name is preceded by
::, as in::AorX::A, then use qualified name lookup. First look upX, if it exists (if not use the global namespace) then look inside it forA. IfXis a class, andAis not a direct member, then look in all the direct bases ofX. IfAis 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 forAin the namespace the type ofXwas declared in, in the friends ofX, and ifXis a template instantiation, likewise for all the arguments involved. Scopes associated only bytypedefdo 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 namespacedirectives, 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.