非依赖名称的重载解析何时发生,在定义上下文或实例化点?

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编译成功,clang编译失败

非依赖名称的重载解析何时发生,是在定义上下文中还是在实例化点?或者两者都对?

如果我对查找规则的理解是正确的,那么由于 g() 是一个非依赖名称,所以在阶段 2 中不会添加重载集。因此 [=11] 的唯一选择=]在定义点是g(long),Clang是正确的。

我当然会天真地 期望 只考虑 g(long) 给定定义的顺序,但是当模板是涉及...

在这两种情况下。

[temp.res]14.6

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

Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used.

所以两个编译器都是对的。