user1899020
user1899020

Reputation: 13575

function lookup and namespaces

If a function cannot be found in the scope where it is called, looking up in the namespaces of its arguments will follow. I have a few questions.

  1. If there are several arguments in different namespaces, which namespace will be first looked up? Is it the namespace of the first argument?

    f(A::T t, B:U u); // Is namespace A looked up first?
    
  2. More complex for template classes, like

    f(A::T<B::U> t); // Namespace A or B is looked up first?
    

Upvotes: 2

Views: 308

Answers (1)

Actually, there is no order among the namespaces for ADL. All relevant namespaces are searched, and all functions thus found form the set of candidates for overload resolution.

Also note that unlike what you say in the question, ADL is performed even when a function is found by unqualified lookup in the calling scope. The union of unqualified lookup and ADL is then used to find the best overload.

ADL is only suppressed if unqualified lookup at calling scope finds a class member, a non-function, or a block-scope non-using declaration.

The relevant rules are in C++14 3.4.2 [basic.lookup.argdep]. Quoting N4140, bold emphasis mine:

3 Let X be the lookup set produced by unqualified lookup (3.4.1) and let Y be the lookup set produced by argument dependent lookup (defined as follows). If X contains

  • a declaration of a class member, or
  • a block-scope function declaration that is not a using-declaration, or
  • a declaration that is neither a function or a function template

then Y is empty. Otherwise Y is the set of declarations found in the namespaces associated with the argument types as described below. The set of declarations found by the lookup of the name is the union of X and Y.

Upvotes: 5

Related Questions