Your problem isn't really with argument dependent lookup. First of all, argument dependent lookup only possibly enters the picture when doing unqualified looking up of functions. When calling foo1(t)
foo1
is a type and its templated constructor is called. Similarly, foo3(t)
is a qualified lookup because foo3
is an object and the function call operator is looked up in the object's class foo1
. The only place where argument lookup enters the picture is calling foo2(t)
where lookup finds to candidates:
::foo2<adl::Test>(adl::Test const&)
::adl::foo2(adl::Test const&)
These two functions are handed off to overload resolution and since both functions are equally good matches the non-template function wins.
Your question are actually three questions:
- The gory details of name lookup are too broad and, thus, this question is a request for an essay to be written which I ignore.
- You second question expands to three more questions, only one seems relevant:
- Which scopes are searched? When looking up an unqualified function name inside a function definition the rules depend on whether any of the names is a dependent name. If there is no such name (i.e., in non-template code or in template code where the names can be determined in phase one), the name is looked up enclosing namespaces and in namespaces associated with its argument. Otherwise, the name is only looked up in the associated namespaces.
- Can argument dependent lookup be forced? It is always done for unqualified function lookups if there is, at least, one argument but names found otherwise may be better matches. Of course, you need to call an unqualified function otherwise it won't be done.