我在一些生产代码中遇到了一个问题,我将其最小化到以下测试用例:
template<typename T> void intermediate(T t) { func(t); // line 4 ("func not declared in this scope") } namespace ns { struct type {}; } void func(ns::type const & p); // line 11 ("declared here,later") void foo(ns::type exit_node) { intermediate(exit_node); // line 15 ("required from here") }
GCC 4.5编译这个罚款.无论是否使用-std = c 11,4.7和4.9都会生成如下消息:
test.cpp: In instantiation of ‘void intermediate(T) [with T = ns::type]’: test.cpp:15:27: required from here test.cpp:4:5: error: ‘func’ was not declared in this scope,and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] test.cpp:11:6: note: ‘void func(const ns::type&)’ declared here,later in the translation unit
以下三件事情将导致文件成功编译:
>将func(ns :: type)移动到ns命名空间中(允许ADL在ns中找到它)@H_404_11@>将类型移动到全局命名空间(允许ADL在:)中找到它:@H_404_11@摆脱中间,直接从foo调用func
那么…这里发生了什么?海湾合作委员会拒绝这个方案是否合法?为什么在第三个变体(通过foo直接调用func)找到func,但是在实例化的时候,原始变体中不合格查找找不到func?