This one has been confusing me and a few co-workers, but we’ve verified this is an error against about 5 different compilers. All of them return that this small code-snippet is “ambiguous”.
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 returns the following:
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.
Why is this happening? There are no “using” statements in the code. The resolution order shouldn’t include the foo namespace, so why is it searching there? Why is this ambiguous?
It is the argument dependent lookup. The argument to
baris in thefoonamespace, so bar is looked up in that namespace too, resulting in an ambiguity. If you want to call thefoofrom the global namespace unambigously call::foo.