473,769 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this ambiguous?

Consider the following code, with the interesting lines numbered in
comments:

class A
{
public :

bool f(int level = 1) // line 5
{ return true ; }

void f(bool val, int level = 1)
{}

} ;

class B
{
private :
A *a ;
public :
bool f(int level = 1) // line 18
{ return a->f(level) ; } // line 19

void f(bool val, int level = 1)
{ a->f(val, level) ; }
} ;
This compiles fine for me (gcc 3.4.4). However, if I make the functions
on line 5 and line 18 const, then my compiler says the function call on
line 19 is ambiguous. It makes sense to me why it would be ambiguous
(level is convertible to bool, and so it might could match f(bool, int)
since the int parameter has a default value). However, what I don't
understand is why it is NOT ambiguous without the const. Any insight?

-Alan
Jul 23 '05 #1
1 2860
* Alan Johnson:
Consider the following code, with the interesting lines numbered in
comments:

class A
{
public :

bool f(int level = 1) // line 5
{ return true ; }

void f(bool val, int level = 1)
{}

} ;

class B
{
private :
A *a ;
public :
bool f(int level = 1) // line 18
{ return a->f(level) ; } // line 19

void f(bool val, int level = 1)
{ a->f(val, level) ; }
} ;
This compiles fine for me (gcc 3.4.4). However, if I make the functions
on line 5 and line 18 const,
The one on line 18 doesn't matter.
then my compiler says the function call on
line 19 is ambiguous. It makes sense to me why it would be ambiguous
(level is convertible to bool, and so it might could match f(bool, int)
since the int parameter has a default value). However, what I don't
understand is why it is NOT ambiguous without the const. Any insight?


First, be clear that result types are not considered in overload
resolution, and that making the function on line 18 const does not
make the object pointed to by 'a' const.

Without the const on line 5 the compiler has a choice between no
conversion, and conversion of the argument to bool. The no-conversion
is clearly better. Hence the first function is selected.

With the const on line 5 either 'a*' must be converted to
'a const*', or int must be converted to bool. I don't recall the
exact rules, but at least there are now two possible conversions. It
surprises me that the to-const conversion should weight in as much
as a possible value representation change (and a lossy one, at that!),
but then, the C++ rules are evolved beasts with lots of non-optimum
and counter-intuitive consequences.

--
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 #2

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

Similar topics

16
1769
by: REH | last post by:
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;
9
3975
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
1
10009
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type A, and the second one of type B. Let say, these are not my classes and I know nothing about the implementation. As system doesn't know what code must be used for resolving the language construction a+b (where A a; and B b;), it returns "The call...
9
13278
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include <hash_map>//from Standard template library //and some other headers
3
1878
by: Arpan | last post by:
The following code exists in a class file named "Users.vb": Namespace Users Public Class UserDetails Public FirstName As String Public LastName As String Public UserName As String Public Password As String Public UserID As String End Class
1
1527
by: rn5a | last post by:
Consider the following code in a VB class file: Namespace LoginUserFetchDB Public Class ZForZebra : Inherits SoapHeader Public UserName As String Public Password As String End Class Public Class GetDBRecords : Inherits WebService Private sqlCmd As SqlCommand
8
3103
by: xtrigger303 | last post by:
Hi to all, I'm working on a smart pointer implementation and I'm trying to get automatic type conversion between different pointer types. I stumbled upon something weird (at least for me) I summarized it in the code below. I was expecting both things at the end to work or not work at all.... Any insight? Thanks in advance, Francesco
12
2046
by: Nathan Sokalski | last post by:
I have several CustomControls that I have written for my project. However, when I try to compile I recieve the following warning & errors: Warning 32 Could not resolve this reference. Could not locate the assembly "nathansokalski_com_test, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors....
3
5823
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as long long int), a decimal (double), or a string (std::string). I basically want to overload most of a Message's operators so that the Message class feels more like a native type. For example... Message msg = "tester"; std::string str = msg;...
0
9589
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
9423
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
10050
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...
1
9999
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
9866
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
7413
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.