Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ ambiguous call to functions in different namespaces

Tags:

c++

filenames

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?

like image 283
Jonathan Sternberg Avatar asked Apr 19 '12 19:04

Jonathan Sternberg


1 Answers

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.

like image 184
juanchopanza Avatar answered Oct 11 '22 20:10

juanchopanza