這個問題讓我和一些同事感到困惑,但是我們已經證實這是對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