473,795 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compiler unable to choose proper overloaded function (causes C2666)

If you have two overloaded functions:

double maximum(double a, double b, double c);
int maximum(int a, int b, int c);

They work fine if you call maximum() with all arguments as doubles, or
all as ints. But, if you mix up doubles with ints in the argument
list, it doesn't know which maximum() to call... but only one could
possibly match -- the one that takes doubles.

I assume this is not a bug, and is by definition... but, why?

Jason

Jul 26 '07 #1
15 2039
Jason Doucette wrote:
If you have two overloaded functions:

double maximum(double a, double b, double c);
int maximum(int a, int b, int c);

They work fine if you call maximum() with all arguments as doubles, or
all as ints. But, if you mix up doubles with ints in the argument
list, it doesn't know which maximum() to call... but only one could
possibly match -- the one that takes doubles.

I assume this is not a bug, and is by definition... but, why?
Floating-integral conversions (from a double to an int) and floating
point conversions (from an int to a double) have the same rank as far
as overload resolution is concerned.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #2
Floating-integral conversions (from a double to an int) and floating
point conversions (from an int to a double) have the same rank as far
as overload resolution is concerned.
Ok. I didn't think that implicit conversions from a double to an int
were *ever* allowed, since they can lose information.

Jason

Jul 26 '07 #3
Jason Doucette wrote:
>Floating-integral conversions (from a double to an int) and floating
point conversions (from an int to a double) have the same rank as far
as overload resolution is concerned.

Ok. I didn't think that implicit conversions from a double to an int
were *ever* allowed, since they can lose information.
If something potentially dangerous would always be disallowed, we'd not
have many feature of the language. Just imagine dereferencing a pointer
not allowed because (Oh, no!) the pointer can be invalid!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #4
Jason Doucette wrote:
>Floating-integral conversions (from a double to an int) and floating
point conversions (from an int to a double) have the same rank as far
as overload resolution is concerned.

Ok. I didn't think that implicit conversions from a double to an int
were *ever* allowed, since they can lose information.
It's worse than that. The conversion from double to int discards the
fractional part, but if the resulting integer is outside the bounds of int,
it's undefined behaviour.

--
rbh
Jul 26 '07 #5
Robert Bauck Hamar wrote:
Jason Doucette wrote:
>>Floating-integral conversions (from a double to an int) and floating
point conversions (from an int to a double) have the same rank as
far as overload resolution is concerned.

Ok. I didn't think that implicit conversions from a double to an int
were *ever* allowed, since they can lose information.

It's worse than that. The conversion from double to int discards the
fractional part, but if the resulting integer is outside the bounds
of int, it's undefined behaviour.
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '07 #6
Robert Bauck Hamar wrote:
Jason Doucette wrote:
>>Floating-integral conversions (from a double to an int) and floating
point conversions (from an int to a double) have the same rank as
far as overload resolution is concerned.

Ok. I didn't think that implicit conversions from a double to an int
were *ever* allowed, since they can lose information.

It's worse than that. The conversion from double to int discards the
fractional part, but if the resulting integer is outside the bounds
of int, it's undefined behaviour.
I am not sure in what sense it's a loss of information, actually. The
conversion is well-defined, it discards the fractional part and retains
the integral part, in the same fashion as when you divide two integers
the result is [essentially] rounded to the next integer toward zero.
Is that a loss of information? <shrug>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '07 #7
On Jul 27, 12:04 am, Jason Doucette <jdouce...@gmai l.comwrote:
Floating-integral conversions (from a double to an int) and floating
point conversions (from an int to a double) have the same rank as far
as overload resolution is concerned.
Ok. I didn't think that implicit conversions from a double to an int
were *ever* allowed, since they can lose information.
It's a serious bug in the standard. There have been several
proposals to "deprecate" such implicit conversions (one by
Stroustrup himself, I think), but for whatever reasons, they
never got anywhere.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 27 '07 #8
Ok. I didn't think that implicit conversions from a double to an int
were *ever* allowed, since they can lose information.

If something potentially dangerous would always be disallowed, we'd not
have many feature of the language. Just imagine dereferencing a pointer
not allowed because (Oh, no!) the pointer can be invalid!
Your example is on a totally different level.

Everyone knows almost every floating point value converted to an
integer results in a significant loss of information. Most languages
disallow this implicit casting from floating point to integer. I
would assume C / C++ did this also. In fact, it does -- you cannot
assign a floating point value to an integer.

So, it's intuitive to think that this would not be considered within
the overload resolution... I am still dumbfounded as to why it does.

Jason

Jul 27 '07 #9
It's worse than that. The conversion from double to int discards the
fractional part, but if the resulting integer is outside the bounds of int,
it's undefined behaviour.
Are you positive about that? I thought it set it to the maximum /
minimum integer value... In my programs, if there's a case a floating
point will be out of integer range, I check before the cast, just in
case. I don't think I've ever tested what would happen if I didn't...

Jason

Jul 27 '07 #10

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

Similar topics

4
3205
by: Roy Yao | last post by:
Why the following code let my compiler complain an overloaded function Init()? // code begin template<class T> class BicircularList { template<class T> class Iterator; template<class T> class ChainNode { friend Iterator<T>;
6
1836
by: PengYu.UT | last post by:
Hi, I run into error with the following program. Would you please help me? Best wishes, Peng struct tag1{}; struct tag2{};
4
1840
by: Vish | last post by:
Hi all, I am having a build error in one of the overloaded functions in my class. The function takes either a string as a parameter or a type referenced in another dll as a parameter. My class references the dll where the type of the parameter is defined. I am able to create to create my class in a form without any build errors and without any references to the DLL where the other parameter type is defined. The form only uses the...
1
2546
by: siegfried | last post by:
The std::lower_bound function requires a function pointer as its last argument. This is simple if you don't overload. How do I I call std::lower_bound with the last argument a function pointer when I need to pass a certain instance of an overloaded function? Thanks, Siegfried
5
10995
by: rolandz | last post by:
Hi, Maybe somebody has been fighting with the problem that I do, currently. I have a class that has method f(). The two versions of the f() method accept different objects: Int and Short. These objects have constructors that allow implicit conversions from simple types. All this has been defined as follows: <code> class Int
6
3828
by: c1t1z3n | last post by:
hiya, i'm having this weird error on my project. As far as I know "ambiguous call to overloaded function" should only occur when the compiler must choose from several methods, but here i don't think it's the case. I've got the following class: class item { public: item(string id_,string datatype_); //item(string id_,string datatype_,int periodo_); //item(string id_,string datatype_,string headerfile_); //item(string id_, string...
1
2959
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; void print(char c) { cout << "from print(char c) : " << c << endl; return;
4
2893
by: Noah Roberts | last post by:
I am trying to keep a vector of tuples sorted based on the first member and so for my insert function I am calling std::lower_bound with a bound comparator like the following: std::lower_bound(sit, eit, t, boost::bind(tuple_t::get<0>(), _1) < boost::bind(tuple_t::get<0>(), _2)); The problem is that get<is overloaded via const. There are the two definitions:
4
2916
by: Joseph Turian | last post by:
I have a templated class with the following methods: Vocab(const T& t); Vocab(unsigned uid); However, when T = unsigned, and I call Vocab(unsigned(0)) then the compiler rightly complains about an ambiguous call to the overloaded function. How can I specify which method I want called in this case? Thanks,
0
9672
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
9519
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
10214
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
10164
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,...
1
7538
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
6780
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
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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

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.