Why isnan's call is NOT ambiguous? keyword aka by entering 2 times similar function declarations

The following code compiles and generates a call std::isnan, see here

#include <iostream>
#include <cmath>

namespace foo {
    template<class T> 
    inline bool isnan (T x)
    { //Fake function returning always true
       std::cout << "foo::isnan" << std::endl;
       return true;
    }
}

using foo::isnan;
using std::isnan;

int main () {
    std::cout << isnan(5.5f) << std::endl;
    return 0;
}

Why is this feature not ambiguous?

Looking at cmath.h, I see:

  template<typename _Tp>
    inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
                       int>::__type
    isnan(_Tp __f)
    {
      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
      return __builtin_isnan(__type(__f));
    }

which, at least for me, doesn’t look like necessarily the best match.

The motivation for this question is:

I found these lines of code (perhaps the result of some Eclipse auto-generation)

using std::isnan
using boost::math::isnan

And I was wondering why the compiler would not generate at least a warning.

+4
source share
1 answer

, , #include <cmath>:

bool isnan( float arg );
bool isnan( double arg );
bool isnan( long double arg );
bool isnan( Integral arg );

, . ( 26,8/10 N4141 , .)

, ​​, , using std::isnan; . ( gcc5 .) using , .

, clang , . , .


, .

using, - , . . , using, , .


, . , , - , , ,... double .

+5

Source: https://habr.com/ru/post/1665608/


All Articles