When does overload resolution of non-dependent name take place, in definition context or point of instantiation?

隐身守侯 提交于 2019-12-07 09:04:31

问题


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?


回答1:


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.




回答2:


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...



来源:https://stackoverflow.com/questions/34154770/when-does-overload-resolution-of-non-dependent-name-take-place-in-definition-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!