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 bar
is in the foo
namespace, so bar is looked up in that namespace too, resulting in an ambiguity. If you want to call the foo
from the global namespace unambigously call ::foo
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With