473,511 Members | 16,830 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 2004
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...@gmail.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 objektorientierter Datenverarbeitung
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
I am not sure in what sense it's a loss of information, actually.

You can't be serious? If I convert a floating point value of pi to an
integer, and get 3, then I've lost the fractional part -- that's the
information that's lost. I don't think anyone would disagree that
information has been lost. It's no different from converting a 32-bit
integer to a byte, you may lose information, since the upper 24-bits
may have had 1's in them that are now no longer stored. That's a lose
of information.

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.
The conversion is well defined. That doesn't imply information is not
lost.

Is that a loss of information? <shrug>
Yes, of course, it is.

Jason

Jul 27 '07 #11
It's a serious bug in the standard.

Agreed.

There have been several
proposals to "deprecate" such implicit conversions (one by
Stroustrup himself, I think), but for whatever reasons, they
never got anywhere.
Wow.

Jason

Jul 27 '07 #12
Jason Doucette wrote:
>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...
The quote from the standard (§4.8):
An rvalue of a floating point type can be converted to an rvalue of an
integer type. The conversion truncates; that is, the fractional part is
discarded. The behaviour is undefined if the truncated value cannot be
represented in the destination type.

--
rbh
Jul 27 '07 #13
Jason Doucette wrote:
>I am not sure in what sense it's a loss of information, actually.

You can't be serious? If I convert a floating point value of pi to an
integer, and get 3, then I've lost the fractional part
I think you confuse the word "lost" with the word "discarded". The
former implies the unwanted result due to having no control over the
outcome of some events.

I believe most of the complaints about the language "inadequacies" stem
from the inability of some people to take responsibility to learn and
consciously use (or not use) certain features.
-- that's the
information that's lost. I don't think anyone would disagree that
information has been lost.
*I* disagree. So you think wrong.
It's no different from converting a 32-bit
integer to a byte, you may lose information, since the upper 24-bits
may have had 1's in them that are now no longer stored. That's a lose
of information.
No, it's not.
>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.

The conversion is well defined. That doesn't imply information is not
lost.
Yes, it does. If you know what the conversion does and you don't want
to "lose" the information, [read my lips:] do not use the conversion.
>Is that a loss of information? <shrug>

Yes, of course, it is.
<shrugWhatever.

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 #14
On Jul 27, 10:49 am, Robert Bauck Hamar <roberth+n...@ifi.uio.no>
wrote:
Jason Doucette wrote:
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...

The quote from the standard (§4.8):
An rvalue of a floating point type can be converted to an rvalue of an
integer type. The conversion truncates; that is, the fractional part is
discarded. The behaviour is undefined if the truncated value cannot be
represented in the destination type.
Thanks, Robert! Can't be more clear than that...

Jason

Jul 27 '07 #15
I think you confuse the word "lost" with the word "discarded". The
former implies the unwanted result due to having no control over the
outcome of some events.
I think this is being pedantic... what the writers mean by "lost" is
that the information has disappeared, and you cannot get it back.
It's unfortunate that programmers don't use proper English, since it
happens all the time, and most arguments are over definitions and not
an actual problem...

(...it is possible to lose something, but still have control over the
outcome of events, if you're not careful... perhaps that's why they
use that term? It's hard to say.)

I believe most of the complaints about the language "inadequacies" stem
from the inability of some people to take responsibility to learn and
consciously use (or not use) certain features.
I generally agree, but sometimes I think it's nice that the language
protects you as well -- for instance, some languages use the alternate
slash for integer division (and complains if you use the regular one,
which is reserved for floating point), which *forces* you to realize
what you are doing, and that the result will be an integer, and not a
floating point value... and then you don't get bit. Even I, after
many years of programming C, have been bitten by integer division,
since it's not something I use often, and some languages give the
result as floating point, which I have assumed at times...

Jason

Jul 27 '07 #16

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

Similar topics

4
3192
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...
6
1810
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
1829
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...
1
2518
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...
5
10976
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...
6
3805
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...
1
2943
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
2878
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: ...
4
2902
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...
0
7242
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,...
0
7138
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...
0
7355
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,...
0
7423
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...
0
5668
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,...
1
5066
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...
0
3225
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.