overload resolution

S

Stefan Ram

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. )«.
 
V

Victor Bazarov

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
 
S

Stefan Ram

Victor Bazarov said:
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.«
 
M

Marc

Stefan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top