2

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.

L. F.
  • 19,445
  • 8
  • 48
  • 82
xmh0511
  • 7,010
  • 1
  • 9
  • 36
  • `0` is an integer value. Why is there ambiguity here? Compare with `func(0.0)`. – tadman Apr 23 '20 at 05:11
  • @tadman I do not talk about overload resolution here,I just say the name lookup,according to the **unqualified lookup** – xmh0511 Apr 23 '20 at 05:12
  • I'm just responding to your code example where you're not using it in an unqualified way. Maybe you mean `func(int i = 0)` and `func(double i = 0.0)` where `func()` is unqualified? I'm not sure from your specific question, the wording is a bit hazy. – tadman Apr 23 '20 at 05:27
  • @tadman why do you think the `func(0)` in my example is not used by an unqualified way? – xmh0511 Apr 23 '20 at 05:39
  • `The lookup for an unqualified name used as the postfix-expression of a function call is described in [basic.lookup.argdep].` – super Apr 23 '20 at 06:53
  • @super But,the rules in [basic.lookup.argdep] refere to **unqualified lookup** – xmh0511 Apr 23 '20 at 07:25

1 Answers1

3

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • A wonderful answers.Do you mean that these order lists are for scopes?So,if it like this,how to interpret that a name of varible exists more than one in the same scope?how to hide the name before one?thanks – xmh0511 Apr 23 '20 at 09:17
  • Correct; the compiler first makes an ordered list of candidate scopes, and then it goes through the scopes one by one until it finds a scope which contains _at least_ one declaration of the name. Of course, a _variable_ name can occur at most once in a scope. Only functions have overloading. – MSalters Apr 23 '20 at 09:42
  • Thanks,I have got it – xmh0511 Apr 23 '20 at 09:58
  • In addition,Does the remain of [basic.lookup.unqual] describle what these candiate scopes are?For example,"A name used in global scope, outside of any function, class or user-declared namespace, shall be declared before its use in global scope.",It means that the "candiate scope" of such name is the global scope before where the name used,Right? – xmh0511 Apr 23 '20 at 13:43