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

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


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?


Solution

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