point of instantiation and name binding

∥☆過路亽.° 提交于 2019-11-30 22:52:59

f(t) is a dependent unqualified function call expression, so only functions found within the definition context and those found via ADL are candidates. f(int) is visible within the definition context, but not f(double), so overload resolution resolves to f(int) for both calls.

f(double) cannot be found by ADL because built-in types have no associated classes or namespaces. If you passed in an argument of class type, and there was an overload of f taking this type, ADL will be able to find it. For example:

void f(int);

template <typename T>
void g(T t)
{
    f(t);
}

class A {};
void f(double);
void f(A);

int main()
{
    g(1);   // calls f(int)
    g(1.1); // calls f(int)
    g(A{}); // calls f(A)
}

f(A) is called because it is located in the global namespace, and A's associated namespace set is the global namespace.

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