473,785 Members | 2,481 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 1769
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
2379
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
2861
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
3976
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
2734
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
9471
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
1329
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
1782
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
2124
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
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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
10325
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...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
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
5381
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
4050
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
2879
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.