Why is this not ambiguous?

Discussion in 'C++' started by REH, Mar 30, 2005.

  1. REH

    REH Guest

    Can some tell me why the chooses the constructor in class B over operator B
    in class A? Is this not ambiguous?

    Thanks.


    #include <iostream>

    using namespace std;

    struct A;

    struct B {
    B() {}
    B(const A&) {} // this is choosen
    };

    struct A {
    A() {}
    operator B() const {return B();} // this is not
    };

    int main()
    {
    A a;
    B b;

    b = a; // ambiguous?
    }
     
    REH, Mar 30, 2005
    #1
    1. Advertising

  2. REH wrote:
    > Can some tell me why the chooses the constructor in class B over operator B
    > in class A? Is this not ambiguous?


    I am not sure I understand the first question. The program is ill-formed
    because the "b = a" _is_ in fact ambiguous.

    >
    > Thanks.
    >
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > struct A;
    >
    > struct B {
    > B() {}
    > B(const A&) {} // this is choosen
    > };
    >
    > struct A {
    > A() {}
    > operator B() const {return B();} // this is not
    > };
    >
    > int main()
    > {
    > A a;
    > B b;
    >
    > b = a; // ambiguous?
    > }



    V
     
    Victor Bazarov, Mar 30, 2005
    #2
    1. Advertising

  3. REH

    Howard Guest

    "REH" <> wrote in message
    news:d2ejdb$...
    > Can some tell me why the chooses the constructor in class B over operator
    > B
    > in class A? Is this not ambiguous?
    >
    > Thanks.
    >
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > struct A;
    >
    > struct B {
    > B() {}
    > B(const A&) {} // this is choosen
    > };
    >
    > struct A {
    > A() {}
    > operator B() const {return B();} // this is not
    > };
    >
    > int main()
    > {
    > A a;
    > B b;
    >
    > b = a; // ambiguous?
    > }
    >


    I think the answer is given in Stroustrup's "The C++ Programming Language",
    p277: "User-defined conversions are considered only if they are neccessary
    to resolve a call." I don't have the Standard to back up that statement,
    but if he's correct, then the fact that a copy-constructor exists that can
    handle the job negates the need to bother looking at other alternatives.
    Thus, no ambiguity is encountered.

    -Howard
     
    Howard, Mar 30, 2005
    #3
  4. * REH:
    > Can some tell me why the chooses the constructor in class B over operator B
    > in class A? Is this not ambiguous?
    >
    > Thanks.
    >
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > struct A;
    >
    > struct B {
    > B() {}
    > B(const A&) {} // this is choosen
    > };
    >
    > struct A {
    > A() {}
    > operator B() const {return B();} // this is not
    > };
    >
    > int main()
    > {
    > A a;
    > B b;
    >
    > b = a; // ambiguous?
    > }


    Comeau online compilation:

    Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    "ComeauTest.c", line 18: error: more than one user-defined conversion from "A"
    to
    "const B" applies:
    function "A::eek:perator B() const"
    function "B::B(const A &)"
    b = a; // ambiguous?
    ^

    1 error detected in the compilation of "ComeauTest.c".

    In strict mode, with -tused, Compile failed

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 30, 2005
    #4
  5. REH

    Grant Guest

    As far as the microsoft VS.NET 2003 compiler is concerned this is an
    error.

    Grant
     
    Grant, Mar 30, 2005
    #5
  6. REH

    REH Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    > * REH:
    > > Can some tell me why the chooses the constructor in class B over

    operator B
    > > in class A? Is this not ambiguous?
    > >
    > > Thanks.
    > >
    > >
    > > #include <iostream>
    > >
    > > using namespace std;
    > >
    > > struct A;
    > >
    > > struct B {
    > > B() {}
    > > B(const A&) {} // this is choosen
    > > };
    > >
    > > struct A {
    > > A() {}
    > > operator B() const {return B();} // this is not
    > > };
    > >
    > > int main()
    > > {
    > > A a;
    > > B b;
    > >
    > > b = a; // ambiguous?
    > > }

    >
    > Comeau online compilation:
    >
    > Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    > Copyright 1988-2003 Comeau Computing. All rights reserved.
    > MODE:strict errors C++
    >
    > "ComeauTest.c", line 18: error: more than one user-defined conversion from

    "A"
    > to
    > "const B" applies:
    > function "A::eek:perator B() const"
    > function "B::B(const A &)"
    > b = a; // ambiguous?
    > ^
    >
    > 1 error detected in the compilation of "ComeauTest.c".
    >
    > In strict mode, with -tused, Compile failed
    >

    That's interesting. Even compiling with all warnings, GCC failed to
    complain. I would file a bug report, but I'm not sure which is correct. I
    just trying it with VC 2003 ToolKit, and says:

    error C2679: binary '=' : no operator found which takes a right-hand operand
    of type 'A' (or there is no acceptable conversion)

    Which I think is wrong, or at least misleading. I guess I will have to look
    in the standard, which is hard for me to comprehend.
     
    REH, Mar 30, 2005
    #6
  7. REH

    REH Guest

    "Victor Bazarov" <> wrote in message
    news:qOA2e.58433$01.us.to.verio.net...
    > REH wrote:
    > > Can some tell me why the chooses the constructor in class B over

    operator B
    > > in class A? Is this not ambiguous?

    >
    > I am not sure I understand the first question. The program is ill-formed
    > because the "b = a" _is_ in fact ambiguous.
    >

    Sorry, that first question should have read, "Can some tell me why then
    compiler chooses the constructor in class B over operator B in class A?"
    Meaning, instead of complaining about the ambiguity, my compiler, given the
    choice of:

    1) B::B(const A&);
    2) A::eek:perator B() const;

    It chose option #1.
     
    REH, Mar 30, 2005
    #7
  8. REH wrote:
    > "Alf P. Steinbach" <> wrote in message
    >> "ComeauTest.c", line 18: error: more than one user-defined
    >> conversion from "A" to
    >> "const B" applies:
    >> function "A::eek:perator B() const"
    >> function "B::B(const A &)"
    >> b = a; // ambiguous?


    > That's interesting. Even compiling with all warnings, GCC failed to
    > complain.


    Get a newer gcc; version 3.4.2 spots the ambiguity.

    Ali
     
    =?utf-8?Q?Ali_=C3=87ehreli?=, Mar 30, 2005
    #8
  9. REH

    REH Guest

    "Ali Çehreli" <> wrote in message
    news:...
    > REH wrote:
    > > "Alf P. Steinbach" <> wrote in message
    > >> "ComeauTest.c", line 18: error: more than one user-defined
    > >> conversion from "A" to
    > >> "const B" applies:
    > >> function "A::eek:perator B() const"
    > >> function "B::B(const A &)"
    > >> b = a; // ambiguous?

    >
    > > That's interesting. Even compiling with all warnings, GCC failed to
    > > complain.

    >
    > Get a newer gcc; version 3.4.2 spots the ambiguity.
    >
    > Ali
    >


    Um, I *am* using 3.4.2. What is your targetted for?
     
    REH, Mar 30, 2005
    #9
  10. REH

    REH Guest

    "REH" <> wrote in message
    news:d2evpm$...
    >
    > "Ali Çehreli" <> wrote in message
    > news:...
    > > Get a newer gcc; version 3.4.2 spots the ambiguity.
    > >
    > > Ali
    > >

    >
    > Um, I *am* using 3.4.2. What is your targetted for?
    >
    >

    I just tried it with version 3.4.3, and it still accepts it.
     
    REH, Mar 30, 2005
    #10
  11. REH wrote:
    > "Ali ehreli" <> wrote in message
    > news:...


    >> Get a newer gcc; version 3.4.2 spots the ambiguity.


    > Um, I *am* using 3.4.2. What is your targetted for?


    It turns out, the compiler option that catches this is

    -pedantic

    Ali
     
    =?utf-8?Q?Ali_=C3=87ehreli?=, Mar 30, 2005
    #11
  12. REH

    REH Guest

    "Ali Çehreli" <> wrote in message
    news:...
    > REH wrote:
    > > "Ali ehreli" <> wrote in message
    > > news:...

    >
    > >> Get a newer gcc; version 3.4.2 spots the ambiguity.

    >
    > > Um, I *am* using 3.4.2. What is your targetted for?

    >
    > It turns out, the compiler option that catches this is
    >
    > -pedantic
    >
    > Ali
    >


    Thanks. I assumed that "-ansi" would catch it, but it does not.
     
    REH, Mar 30, 2005
    #12
  13. REH wrote:

    > That's interesting. Even compiling with all warnings, GCC failed to
    > complain.



    C:\c>g++ -std=c++98 -pedantic-errors -Wall temp.cpp -o temp.exe
    temp.cpp: In function `int main()':
    temp.cpp:22: error: conversion from `A' to `const B' is ambiguous
    temp.cpp:14: note: candidates are: A::eek:perator B() const
    temp.cpp:9: note: B::B(const A&)

    C:\c>




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Mar 31, 2005
    #13
  14. REH wrote:

    > Thanks. I assumed that "-ansi" would catch it, but it does not.


    The complete g++ options that I am using may help:


    -std=c++98 -pedantic-errors -Wall -O3 -ffloat-store -mtune=pentium3



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Mar 31, 2005
    #14
  15. REH

    REH Guest

    [OT] Re: Why is this not ambiguous?

    "Ioannis Vranos" <> wrote in message
    news:1112248707.384234@athnrd02...
    > REH wrote:
    >
    > > Thanks. I assumed that "-ansi" would catch it, but it does not.

    >
    > The complete g++ options that I am using may help:
    >
    >
    > -std=c++98 -pedantic-errors -Wall -O3 -ffloat-store -mtune=pentium3
    >
    >
    >
    > --
    > Ioannis Vranos
    >
    > http://www23.brinkster.com/noicys


    Thanks. As another poster informed me, it was "-pedantic" that does the
    trick. Though, I don't understand why you have to utilize an option to get
    GCC to report this as an error.

    REH
     
    REH, Mar 31, 2005
    #15
  16. Re: [OT] Re: Why is this not ambiguous?

    REH wrote:

    > Thanks. As another poster informed me, it was "-pedantic" that does the
    > trick. Though, I don't understand why you have to utilize an option to get
    > GCC to report this as an error.



    GCC considers itself smarter than this, I guess. :) Or in other words, it considers that
    the copy (=conversion) constructor of the type on the left that is assigned the value of
    the type on the right, makes sense to be used (or in other words, it considers that type
    conversions (copy constructors) have more priority than compatibility operators).

    Theoretically speaking, both should have the same end result. But I think no one would
    define both of them on purpose, so here we are. :)



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Mar 31, 2005
    #16
  17. "Howard" <> wrote in message news:<s_A2e.24747$>...
    >
    > I think the answer is given in Stroustrup's "The C++ Programming Language",
    > p277: "User-defined conversions are considered only if they are neccessary
    > to resolve a call." I don't have the Standard to back up that statement,
    > but if he's correct, then the fact that a copy-constructor exists that can
    > handle the job negates the need to bother looking at other alternatives.
    > Thus, no ambiguity is encountered.
    >
    > -Howard


    The Standard says, in 12.3-2:
    User-defined conversions are applied only where they are unambiguous
    (10.2, 12.3.2).

    Thus, I believe it's an error to provide the two conversions, because
    they are always ambigous and thus could not be applied.

    On the other hand, The Standard says, in 12.3.2-1:
    "... Classes, enumerations, and typedef-names shall not be declared in
    the typespecifier-seq. ..."

    So I believe that the conversion operator to B is invalid.

    I may be interpreting The Standard wrongly, though.

    Regards,

    Marcelo Pinto.
     
    Marcelo Pinto, Mar 31, 2005
    #17
    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. sks_cpp
    Replies:
    8
    Views:
    354
    tom_usenet
    Aug 26, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    935
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,128
    Smokey Grindel
    Dec 2, 2006
  4. a eriksson
    Replies:
    1
    Views:
    307
    Victor Bazarov
    Aug 9, 2005
  5. Tim H

    Why is this ambiguous?

    Tim H, Dec 19, 2007, in forum: C++
    Replies:
    3
    Views:
    362
Loading...

Share This Page