【发布时间】:2013-04-07 00:44:15
【问题描述】:
这样编译:
struct str {};
namespace a
{
void foo(str s) {}
}
namespace b
{
void foo(str s) {}
void bar(str s) { foo(s); }
}
int main(int, char**)
{
return 0;
}
但这没有(结构定义移动到命名空间 a 中)
namespace a
{
struct str {};
void foo(str s) {}
}
namespace b
{
void foo(a::str s) {}
void bar(a::str s) { foo(s); }
}
int main(int, char**)
{
return 0;
}
我得到的错误是
bad.cpp: In function ‘void b::bar(a::str)’:
bad.cpp:12: error: call of overloaded ‘foo(a::str&)’ is ambiguous
bad.cpp:10: note: candidates are: void b::foo(a::str)
bad.cpp:5: note: void a::foo(a::str)
似乎合理的预期是,因为 a::foo 不在作用域内,所以对 foo 的调用只能引用 b::foo。 编译失败是否有充分的理由(如果有,是什么原因),还是(两个主要编译器的)实现存在缺陷?
【问题讨论】:
标签: c++ namespaces g++ overloading clang