这个问题困扰了我和几位同事,但我们已经验证过这是针对大约5个不同编译器的错误。所有编译器都返回这个小代码片段是“模棱两可”的。
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);
}
Clang 返回以下内容:
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.
为什么会发生这种情况?代码中没有“using”语句。解析顺序不应包括foo命名空间,那么为什么要在那里搜索?为什么会出现歧义?