namespace - ambiguous call

Discussion in 'C++' started by Vijay Meena, Dec 19, 2008.

  1. Vijay Meena

    Vijay Meena Guest

    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
     
    Vijay Meena, Dec 19, 2008
    #1
    1. Advertising

  2. Vijay Meena wrote:
    > 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.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 19, 2008
    #2
    1. Advertising

  3. Vijay Meena

    Noah Roberts Guest

    Andrey Tarasevich wrote:
    > Vijay Meena wrote:
    >> 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.


    AKA "Koenig Lookup" - just in case people want to use google.
     
    Noah Roberts, Dec 19, 2008
    #3
  4. On 19 déc, 18:46, Vijay Meena <> wrote:
    > 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.
     
    Laurent Deniau, Dec 19, 2008
    #4
  5. Andrey Tarasevich wrote:
    >
    >> 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.


    A correction:

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

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 19, 2008
    #5
  6. Vijay Meena

    Vijay Meena Guest

    Thanks all :)
     
    Vijay Meena, Dec 19, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nancy.
    Replies:
    3
    Views:
    8,184
    i. Wiin
    Sep 28, 2004
  2. John
    Replies:
    1
    Views:
    4,813
    Patrice
    Jun 24, 2005
  3. =?Utf-8?B?SGFucyBCYXVtYW5u?=

    'mycontrol_ascx' is ambiguous in namespace ASP

    =?Utf-8?B?SGFucyBCYXVtYW5u?=, Nov 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    484
    =?Utf-8?B?SGFucyBCYXVtYW5u?=
    Nov 23, 2006
  4. Nemisis
    Replies:
    4
    Views:
    713
    Nemisis
    Apr 16, 2007
  5. g@work
    Replies:
    2
    Views:
    1,685
    g@work
    Jul 4, 2007
Loading...

Share This Page