这个问题让我和一些同事感到困惑,但是我们已经证实这是对5种不同编译器的错误。他们都回来说这个小代码片段是“不明确的”。C++对不同命名空间中的函数的模糊调用
namespace foo {
struct type_t {
int x;
};
void bar(type_t &);
}
void bar(foo::type_t &);
void func(void) {
foo::type_t x = { 10 };
bar(x);
}
锵返回如下:
func.cpp:12:3: error: call to 'bar' is ambiguous
bar(x);
^~~
func.cpp:5:8: note: candidate function
void bar(type_t &);
^
func.cpp:8:6: note: candidate function
void bar(foo::type_t &);
^
1 error generated.
这究竟是为什么?代码中没有“使用”语句。解析顺序不应该包含foo命名空间,那么它为什么在那里搜索?为什么这是模糊的?
阅读[ADL](http://en.wikipedia.org/wiki/Argument-dependent_name_lookup),也称为Koenig lookup。 – ildjarn 2012-04-19 19:21:33
在gcc中:http://ideone.com/G4uHo – 2012-04-19 19:22:04
这似乎不是一个编译器错误。 g ++,HP C++和Comeau也会产生类似的错误。 – daxelrod 2012-04-19 19:22:58