473,226 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Why is this not ambiguous?

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?
}

Jul 23 '05 #1
16 1720
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
Jul 23 '05 #2

"REH" <bo***@nowhere.net> wrote in message
news:d2*********@cui1.lmms.lmco.com...
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

Jul 23 '05 #3
* 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::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?
Jul 23 '05 #4
As far as the microsoft VS.NET 2003 compiler is concerned this is an
error.

Grant

Jul 23 '05 #5
REH

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* 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::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

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.

Jul 23 '05 #6
REH

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:qO*******************@newsread1.mlpsca01.us.t 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::operator B() const;

It chose option #1.

Jul 23 '05 #7
REH wrote:
"Alf P. Steinbach" <al***@start.no> wrote in message
"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?
That's interesting. Even compiling with all warnings, GCC failed to
complain.


Get a newer gcc; version 3.4.2 spots the ambiguity.

Ali

Jul 23 '05 #8
REH

"Ali Çehreli" <ac******@yahoo.com> wrote in message
news:3b*************@individual.net...
REH wrote:
"Alf P. Steinbach" <al***@start.no> wrote in message
"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?

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?

Jul 23 '05 #9
REH

"REH" <bo***@nowhere.net> wrote in message
news:d2*********@cui1.lmms.lmco.com...

"Ali Çehreli" <ac******@yahoo.com> wrote in message
news:3b*************@individual.net...
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.

Jul 23 '05 #10
REH wrote:
"Ali ehreli" <ac******@yahoo.com> wrote in message
news:3b*************@individual.net...
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

Jul 23 '05 #11
REH

"Ali Çehreli" <ac******@yahoo.com> wrote in message
news:3b*************@individual.net...
REH wrote:
"Ali ehreli" <ac******@yahoo.com> wrote in message
news:3b*************@individual.net...

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.

Jul 23 '05 #12
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::operator B() const
temp.cpp:9: note: B::B(const A&)

C:\c>


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #13
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
Jul 23 '05 #14
REH

"Ioannis Vranos" <iv*@remove.this.grad.com> 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
Jul 23 '05 #15
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
Jul 23 '05 #16
"Howard" <al*****@hotmail.com> wrote in message news:<s_*******************@bgtnsc04-news.ops.worldnet.att.net>...

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.
Jul 23 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
1
by: Alan Johnson | last post by:
Consider the following code, with the interesting lines numbered in comments: class A { public : bool f(int level = 1) // line 5 { return true ; }
9
by: xuatla | last post by:
compile error: test1.cpp:21: error: ISO C++ says that `T mtd::CDiffOperator::getdp(const mtd::mVector&, long int, mtd::mBCTYPE) const' and `void mtd::CDiffOperator::getdp(mtd::mVector&, const...
3
by: Lee Gillie | last post by:
I have a VS6 project which I brought into VS .NET, and all has been building fine. Then I upgraded to VS 2003 and I have one source which will no longer compile. Any clues? Compiling......
0
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
4
by: Johnny van Cadsand | last post by:
Hi, I get the following errors when trying to build my solution: 'Exception' is ambiguous in the namespace 'System' 'EventArgs' is ambiguous in the namespace 'System' 'IDisposable' is ambiguous...
1
by: --== Alain ==-- | last post by:
Hi, Under VC++.net, if i include windows.h, and compile my application, i have several data types ambiguous, like IDataObject, IMessageFilter, IDropTarget,... so it means that they are...
7
by: rn5a | last post by:
This is the second time I am asking this question in this newsgroup since I haven't got a solution or response from anyone in my previous post & I need to resolve this issue desperately. Sorry for...
32
by: Anna Smidt | last post by:
I am having an "ambiguous call to overloaded function" error again. This is the function: int nGetProfWidth (int ncols, unsigned ProfSpec) { if ((ProfSpec & PROF_2d) == 0) return ncols;...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.