我对C++中的标准ADL分辨率有疑问。带模板功能的嵌套命名空间中的C++ ADL
下面是一个示例代码解释我的询问:
#include <string>
// The mechanism:
namespace A {
template< class C >
::std::string scope(const C*)
{ return "A"; }
namespace B {
template< class C >
::std::string scope(const C *foo)
{ return A::scope(foo)+"::B"; }
} // namespace B
} // namespace A
::std::string scope(...)
{ return ""; }
// The test classes
struct foo {};
namespace A {
struct foo {};
namespace B {
struct foo {};
}
}
// The usage
int main()
{
foo *Foo=0;
A::foo *FooA=0;
A::B::foo *FooB=0;
scope(Foo); // OK, returns ""
scope(FooA); // OK, returns "A"
scope(FooB); // On one compiler, OK returns "A::B" ; On another, compiler error "Ambiguous call" between A::scope() and A::B::scope()
}
所以,我的问题是什么是关于ADL的标准? 应该找到参数的父命名空间中的所有函数,还是只有参数(嵌套)名称空间中可用的函数+全局函数?
这个程序已经过测试,于2008年MSVC(与SP编译但不能没有...)
你的代码甚至不会在任何编译器上编译,因为不带参数的'A :: scope()'不存在,但你可以调用这个函数。 – Nawaz 2011-05-12 09:20:37
一个编译器老旧,另一个是新的,还是来自不同的制造商? – 2011-05-12 09:25:25
@Nawaz - >编辑... – DocZeD 2011-05-12 11:19:33