![]() |
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. )«. |
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 |
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.« |
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.