overload resolution

Discussion in 'C++' started by Stefan Ram, Jan 23, 2013.

  1. Stefan Ram

    Stefan Ram Guest

    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. )«.
     
    Stefan Ram, Jan 23, 2013
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jan 23, 2013
    #2
    1. Advertising

  3. Stefan Ram

    Stefan Ram Guest

    Victor Bazarov <> 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.«
     
    Stefan Ram, Jan 23, 2013
    #3
  4. Stefan Ram

    Marc Guest

    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.
     
    Marc, Jan 25, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,876
    Eph0nk
    Oct 23, 2003
  2. =?Utf-8?B?Q2FpbGlu?=
    Replies:
    0
    Views:
    1,614
    =?Utf-8?B?Q2FpbGlu?=
    Sep 9, 2005
  3. Wolfgang Jeltsch

    overload resolution

    Wolfgang Jeltsch, Aug 31, 2003, in forum: C++
    Replies:
    2
    Views:
    415
    Filipe Sousa
    Aug 31, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    384
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    274
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page