Why is this not ambiguous? | | |
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?
} | | | | re: Why is this not ambiguous?
REH wrote:[color=blue]
> Can some tell me why the chooses the constructor in class B over operator B
> in class A? Is this not ambiguous?[/color]
I am not sure I understand the first question. The program is ill-formed
because the "b = a" _is_ in fact ambiguous.
[color=blue]
>
> 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?
> }[/color]
V | | | | re: Why is this not ambiguous?
"REH" <bogus@nowhere.net> wrote in message
news:d2ejdb$ghi2@cui1.lmms.lmco.com...[color=blue]
> 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?
> }
>[/color]
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 | | | | re: Why is this not ambiguous?
* REH:[color=blue]
> 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?
> }[/color]
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::operator 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? | | | | re: Why is this not ambiguous?
As far as the microsoft VS.NET 2003 compiler is concerned this is an
error.
Grant | | | | re: Why is this not ambiguous?
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:424adf02.811159546@news.individual.net...[color=blue]
> * REH:[color=green]
> > Can some tell me why the chooses the constructor in class B over[/color][/color]
operator B[color=blue][color=green]
> > 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?
> > }[/color]
>
> 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[/color]
"A"[color=blue]
> to
> "const B" applies:
> function "A::operator 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
>[/color]
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. | | | | re: Why is this not ambiguous?
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:qOA2e.58433$NC6.23891@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> REH wrote:[color=green]
> > Can some tell me why the chooses the constructor in class B over[/color][/color]
operator B[color=blue][color=green]
> > in class A? Is this not ambiguous?[/color]
>
> I am not sure I understand the first question. The program is ill-formed
> because the "b = a" _is_ in fact ambiguous.
>[/color]
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::operator B() const;
It chose option #1. | | | | re: Why is this not ambiguous?
REH wrote:[color=blue]
> "Alf P. Steinbach" <alfps@start.no> wrote in message[color=green]
>> "ComeauTest.c", line 18: error: more than one user-defined
>> conversion from "A" to
>> "const B" applies:
>> function "A::operator B() const"
>> function "B::B(const A &)"
>> b = a; // ambiguous?[/color][/color]
[color=blue]
> That's interesting. Even compiling with all warnings, GCC failed to
> complain.[/color]
Get a newer gcc; version 3.4.2 spots the ambiguity.
Ali | | | | re: Why is this not ambiguous?
"Ali Çehreli" <acehreli@yahoo.com> wrote in message
news:3b0d2qF6e76viU1@individual.net...[color=blue]
> REH wrote:[color=green]
> > "Alf P. Steinbach" <alfps@start.no> wrote in message[color=darkred]
> >> "ComeauTest.c", line 18: error: more than one user-defined
> >> conversion from "A" to
> >> "const B" applies:
> >> function "A::operator B() const"
> >> function "B::B(const A &)"
> >> b = a; // ambiguous?[/color][/color]
>[color=green]
> > That's interesting. Even compiling with all warnings, GCC failed to
> > complain.[/color]
>
> Get a newer gcc; version 3.4.2 spots the ambiguity.
>
> Ali
>[/color]
Um, I *am* using 3.4.2. What is your targetted for? | | | | re: Why is this not ambiguous?
"REH" <bogus@nowhere.net> wrote in message
news:d2evpm$ghi3@cui1.lmms.lmco.com...[color=blue]
>
> "Ali Çehreli" <acehreli@yahoo.com> wrote in message
> news:3b0d2qF6e76viU1@individual.net...[color=green]
> > Get a newer gcc; version 3.4.2 spots the ambiguity.
> >
> > Ali
> >[/color]
>
> Um, I *am* using 3.4.2. What is your targetted for?
>
>[/color]
I just tried it with version 3.4.3, and it still accepts it. | | | | re: Why is this not ambiguous?
REH wrote:[color=blue]
> "Ali ehreli" <acehreli@yahoo.com> wrote in message
> news:3b0d2qF6e76viU1@individual.net...[/color]
[color=blue][color=green]
>> Get a newer gcc; version 3.4.2 spots the ambiguity.[/color][/color]
[color=blue]
> Um, I *am* using 3.4.2. What is your targetted for?[/color]
It turns out, the compiler option that catches this is
-pedantic
Ali | | | | re: Why is this not ambiguous?
"Ali Çehreli" <acehreli@yahoo.com> wrote in message
news:3b0ivvF6eeuroU1@individual.net...[color=blue]
> REH wrote:[color=green]
> > "Ali ehreli" <acehreli@yahoo.com> wrote in message
> > news:3b0d2qF6e76viU1@individual.net...[/color]
>[color=green][color=darkred]
> >> Get a newer gcc; version 3.4.2 spots the ambiguity.[/color][/color]
>[color=green]
> > Um, I *am* using 3.4.2. What is your targetted for?[/color]
>
> It turns out, the compiler option that catches this is
>
> -pedantic
>
> Ali
>[/color]
Thanks. I assumed that "-ansi" would catch it, but it does not. | | | | re: Why is this not ambiguous?
REH wrote:
[color=blue]
> That's interesting. Even compiling with all warnings, GCC failed to
> complain.[/color]
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::operator B() const
temp.cpp:9: note: B::B(const A&)
C:\c>
--
Ioannis Vranos http://www23.brinkster.com/noicys | | | | re: Why is this not ambiguous?
REH wrote:
[color=blue]
> Thanks. I assumed that "-ansi" would catch it, but it does not.[/color]
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 | | | | re: Why is this not ambiguous?
"Ioannis Vranos" <ivr@remove.this.grad.com> wrote in message
news:1112248707.384234@athnrd02...[color=blue]
> REH wrote:
>[color=green]
> > Thanks. I assumed that "-ansi" would catch it, but it does not.[/color]
>
> 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[/color]
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 | | | | re: Why is this not ambiguous?
REH wrote:
[color=blue]
> 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.[/color]
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 | | | | re: Why is this not ambiguous?
"Howard" <alicebt@hotmail.com> wrote in message news:<s_A2e.24747$cg1.12853@bgtnsc04-news.ops.worldnet.att.net>...[color=blue]
>
> 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[/color]
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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|