Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is this not ambiguous?

Reply
Thread Tools

Why is this not ambiguous?

 
 
REH
Guest
Posts: n/a
 
      03-30-2005
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?
}



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-30-2005
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
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      03-30-2005

"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



 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-30-2005
* 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: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?
 
Reply With Quote
 
Grant
Guest
Posts: n/a
 
      03-30-2005
As far as the microsoft VS.NET 2003 compiler is concerned this is an
error.

Grant

 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      03-30-2005

"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: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.



 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      03-30-2005

"Victor Bazarov" <> wrote in message
news:qOA2e.58433$ o.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:perator B() const;

It chose option #1.



 
Reply With Quote
 
=?utf-8?Q?Ali_=C3=87ehreli?=
Guest
Posts: n/a
 
      03-30-2005
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: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

 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      03-30-2005

"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: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?



 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      03-30-2005

"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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57