基本类型和用户定义类型的名称查找问题

name lookup troubles with fundamental and user defined types

这样编译:

struct type{};

template<typename T>
void foo(T in) { bar(in, type()); }

void bar(int, const type&) {}
int main() { foo(42); }

这不是(正如我在 中了解到的):

template<typename T>
void foo(T in) { bar(in); }

void bar(int) {}
int main() { foo(42); }

第一个片段编译的原因也用ADL解释了吗?如果可以,怎么做?

模板参数是基本类型,ADL 不应该为它工作...为什么使用类型 type 有什么不同?

虽然在您的具体专业化中 in 是基本类型,但 bar 仍然是一个依赖名称,因此其查找的参数依赖部分是在实例化上下文中执行的。使它依赖的参数没有关联的命名空间这一事实是无关紧要的。所有非依赖参数仍然对关联命名空间和 类.

集有贡献