0

这编译:名称查找的烦恼与基本面和用户定义类型

struct type{}; 

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

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

这并不(正如我在previous question from today教训):

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

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

是第一个片段也编译的原因与ADL解释?如果是这样,怎么样?

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

回答

2

尽管在具体的专业in是基本类型,bar仍然是一个独立的名称,因此它的查找的参数依赖部分是在实例化上下文中执行的。事实上,使它依赖的参数没有关联的名称空间是无关紧要的。所有非依赖的参数仍然有助于关联的命名空间和类的集合。