namespace - ambiguous call

V

Vijay Meena

I am reading c++ Faq and not able to understand this question
properly. The code is like -

// File - decl.hpp
namespace MySubsystem {
class Fred { };
void myFunc(Fred& x) { }
}

//File - main.cpp

#include "decl.hpp"

void myFunc(MySubsystem::Fred& x) { }

int main() {
MySubsystem::Fred x;
myFunc(x) //<---- ambiguous call to this function
}

When I compile it then compiler generates error as ambiguous call to
function myFunc. My question is - why not the call myFunc inside main
bind to the function defined in global namespace. Why is it ambiguous
call ?


If i change the code as follow -
// File - decl.hpp
namespace MySubsystem {
class Fred { };
void myFunc(void) { }
}


//File - main.cpp

#include "decl.hpp"

void myFunc(void) { }

int main() {
MySubsystem::Fred x;
myFunc() //<---- _NO_ ambiguous call here
}

Then I do not get any problem. Why now it binds to function defined in
global namespace ?

Thanks
 
A

Andrey Tarasevich

Vijay said:
I am reading c++ Faq and not able to understand this question
properly. The code is like -

// File - decl.hpp
namespace MySubsystem {
class Fred { };
void myFunc(Fred& x) { }
}

//File - main.cpp

#include "decl.hpp"

void myFunc(MySubsystem::Fred& x) { }

int main() {
MySubsystem::Fred x;
myFunc(x) //<---- ambiguous call to this function
}

When I compile it then compiler generates error as ambiguous call to
function myFunc. My question is - why not the call myFunc inside main
bind to the function defined in global namespace. Why is it ambiguous
call ?

It is called ADL - Argument-Dependent Lookup. When argument type if
defined in a namespace, functions from that namespace are also
considered by name lookup. This is what brought 'MySubsystem::myFunc'
into consideration during name lookup for the call in 'main'. Later
overload resolution compared the candidates and decided that the call is
ambiguous.
If i change the code as follow -
// File - decl.hpp
namespace MySubsystem {
class Fred { };
void myFunc(void) { }
}


//File - main.cpp

#include "decl.hpp"

void myFunc(void) { }

int main() {
MySubsystem::Fred x;
myFunc() //<---- _NO_ ambiguous call here
}

Then I do not get any problem. Why now it binds to function defined in
global namespace ?

Now you have no arguments and, therefore, no ADL. Without ADL, the
ordinary name hiding rules the day, which is why there's no ambiguity here.
 
L

Laurent Deniau

I am reading c++ Faq and not able to understand this question
properly. The code is like -

// File - decl.hpp
namespace MySubsystem {
      class Fred { };
      void myFunc(Fred& x) { }

}

//File - main.cpp

#include "decl.hpp"

void myFunc(MySubsystem::Fred& x) { }

int main() {
      MySubsystem::Fred x;
      myFunc(x)   //<---- ambiguous call to this function

}

When I compile it then compiler generates error as ambiguous call to
function myFunc. My question is - why not the call myFunc inside main
bind to the function defined in global namespace. Why is it ambiguous
call ?

If i change the code as follow -
// File - decl.hpp
namespace MySubsystem {
      class Fred { };
      void myFunc(void) { }

}

//File - main.cpp

#include "decl.hpp"

void myFunc(void) { }

int main() {
      MySubsystem::Fred x;
      myFunc()   //<---- _NO_ ambiguous call here

}

Then I do not get any problem. Why now it binds to function defined in
global namespace ?

Thanks

As already mentioned, this is called ADL, or Koenig lookup in the old
days.

http://en.wikipedia.org/wiki/Argument_dependent_name_lookup

This was introduced (from the D&E) to allow code like

std::cout << my_object;

otherwise you would have to write

my_namespace::eek:perator<<(std::cout, my_object);

or import the my_namespace where the operator<< for my_object's class
is defined.

a+, ld.
 
A

Andrey Tarasevich

Andrey said:
Now you have no arguments and, therefore, no ADL. Without ADL, the
ordinary name hiding rules the day, which is why there's no ambiguity here.

A correction:

More precisely, name hiding plays no role in this case. Without ADL,
members of 'MySubsystem' are simply not considered at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top