Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   overload resolution (http://www.velocityreviews.com/forums/t956830-overload-resolution.html)

Stefan Ram 01-23-2013 10:57 AM

overload resolution
 
Was there a change in C++ or are the compilers broken?

For the source code

#include <iostream>
#include <ostream>
#include <cmath>

int main(){ ::std::cout << ::std::cos( 1 )<< '\n'; }

, I expect an error message, since it is not possible to tell
which overload of »::std::cos« (double, float, or long double)
is to be used. But instead, all compilers I tried gave me no
error message, no warning and execute the code as if I had
written »::std::cos( 1. )«.


Victor Bazarov 01-23-2013 02:03 PM

Re: overload resolution
 
On 1/23/2013 5:57 AM, Stefan Ram wrote:
> Was there a change in C++ or are the compilers broken?
>
> For the source code
>
> #include <iostream>
> #include <ostream>
> #include <cmath>
>
> int main(){ ::std::cout << ::std::cos( 1 )<< '\n'; }
>
> , I expect an error message, since it is not possible to tell
> which overload of »::std::cos« (double, float, or long double)
> is to be used.


See 4.9/2. AIUI, it's up to the implementation what floating point type
(float, double, long double) to convert your integral value to. The
"can be converted" and "exact if possible" are the criteria. No other
criteria exist.

> But instead, all compilers I tried gave me no
> error message, no warning and execute the code as if I had
> written »::std::cos( 1. )«.
>


V
--
I do not respond to top-posted replies, please don't ask

Stefan Ram 01-23-2013 02:35 PM

Re: overload resolution
 
Victor Bazarov <v.bazarov@comcast.invalid> writes:
>>int main(){ ::std::cout << ::std::cos( 1 )<< '\n'; }

>See 4.9/2. AIUI, it's up to the implementation what floating point type
>(float, double, long double) to convert your integral value to. The
>"can be converted" and "exact if possible" are the criteria. No other
>criteria exist.


I was thinking along the lines of n3290 13.3.3: There are three
viable functions (double, float, long double), and 13.3.3 p2 says:

»If there is exactly one viable function that is a
better function than all other viable functions,
then it is the one selected by overload resolution;
otherwise the call is ill-formed.«


Marc 01-25-2013 03:11 PM

Re: overload resolution
 
Stefan Ram wrote:

> Was there a change in C++ or are the compilers broken?
>
> For the source code
>
> #include <iostream>
> #include <ostream>
> #include <cmath>
>
> int main(){ ::std::cout << ::std::cos( 1 )<< '\n'; }
>
> , I expect an error message, since it is not possible to tell
> which overload of »::std::cos« (double, float, or long double)
> is to be used. But instead, all compilers I tried gave me no
> error message, no warning and execute the code as if I had
> written »::std::cos( 1. )«.


There was a change in C++11, [c.math] paragraph 11:

Moreover, there shall be additional overloads sufficient to ensure:
1. If any argument corresponding to a double parameter has type long
double, then all arguments corresponding to double parameters are
effectively cast to long double.
2. Otherwise, if any argument corresponding to a double parameter has
type double or an integer type, then all arguments corresponding to
double parameters are effectively cast to double.
3. Otherwise, all arguments corresponding to double parameters are
effectively cast to float.



All times are GMT. The time now is 08:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57