473,910 Members | 7,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1782
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.lmc o.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_EVALUATI ON_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::operato r 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.i ndividual.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_EVALUATI ON_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::operato r 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.********@com Acast.net> wrote in message
news:qO******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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::operato r 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******** *****@individua l.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::operato r 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.lmc o.com...

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

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

Similar topics

4
2383
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
2867
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
3983
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 mtd::mVector&, mtd::mBCTYPE) const' are ambiguous even though the worst conversion for the former is better than the worst conversion for the latter
3
2742
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... DotNetManagedExport.cpp C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcom.h(5529) : error C2872: 'CONNECTDATA' : ambiguous symbol could be 'C:\Program Files\Microsoft Visual Studio .NET
0
278
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
9476
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 in the namespace 'System' I know it has something to do with double references, but i can't find anything... I think it's weird because it's in the namespace 'System'
1
1333
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 defined in windows.h but also in some other namespaces. how can i do to still have my windows.h included and avoiding ambiguous
7
1792
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 the double post but I just didn't have any other option other than re-posting the same question in this newsgroup. Consider the following code in a VB class file: Namespace LoginUserFetchDB Public Class ZForZebra : Inherits SoapHeader
32
2131
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; return int(sqrt(ncols)); //HERE THE ERROR IS THROWN }
0
9879
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9727
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8099
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7250
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5939
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3360
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.