Search code examples
c++language-lawyeroverload-resolutionname-lookup

A confusion about function name lookup


I'm confused by some rules in the standard. I'll cite them here:

[basic.lookup.argdep]:

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

So the sentence above means that the set X is created by unqualified lookup. Then we look at the rules of unqualified lookup:

[basic.lookup.unqual]:

In all the cases listed in [basic.lookup.unqual], the scopes are searched for a declaration in the order listed in each of the respective categories; name lookup ends as soon as a declaration is found for the name.

The emphasized part "name lookup ends as soon as a declaration is found for the name" means that once a name is found, the lookup stops.

So my question is:

void func(int){}
void func(double){}
int main(){
  func(0);
}

Consider the above code. The name of fun is used in an unqualified way. So the unqualified lookup rules are performed. Hence, once either func(double) or func(int) is found, the lookup is stopped. So, why can func be overloaded, i.e. the set of candidate function contains both func(int) and func(double)? Doesn't it contradict the unqualified lookup rules? If I miss something, please correct me.


Solution

  • Reasonable question. The relevant part is "the scopes are searched for a declaration in the order listed".

    In pseudo-code

    for (auto scope: scopes)
    {
       if (scope.contains(name))
          return scope;
    }
    throw ill_formed(name);
    

    As soon as one scope is found that contains name, that scope is selected. Further scopes on the list are not searched. Even if name occurs in that scope, it will not participate in overload resolution.

    In your example however, the selected scope contains not one but two declarations of func so overload resolution still happens.