Reputation: 1285
3.4 [basic.lookup]/p1
Overload resolution (13.3) takes place after name lookup has succeeded.
void g(long);
void g(int, int);
template<class T> void f() { g(0); }
void g(int, int = 0) {}
int main(){
f<int>();
}
gcc compiles succeed, clang faild.
When does overload resolution of non-dependent name take place, in definition context or point of instantiation? Or both are right?
Upvotes: 4
Views: 121
Reputation: 478
In both context.
[temp.res] 14.6\8
If a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter, the program is ill-formed; no diagnostic is required. If the interpretation of such a construct in the hypothetical instantiation is different from the interpretation of the corresponding construct in any actual instantiation of the template, the program is ill-formed; no diagnostic is required.
[temp.nondep] 14.6.3\1
Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used.
So both compilers are right.
Upvotes: 2
Reputation: 16824
If my understanding of the lookup rules are correct, then since g()
is a non-dependent name, the overload set won't be added to in phase 2. Therefore the only choice for g(0)
at the point of definition is g(long)
, and Clang is correct.
I would certainly naively expect only g(long)
to be considered given the ordering of the definitions, but then again the C++ standard isn't always intuitive when templates are involved...
Upvotes: 0