[cfe-dev] [libc++] std::abs for floating point values

Luc Bourhis luc_j_bourhis at mac.com
Thu May 3 07:37:51 PDT 2012


using the version of libc++ installed on Lion by XCode 4.3, <cmath> defines std::abs as follow

template <class _A1>
typename enable_if<is_floating_point<_A1>::value, _A1>::type
abs(_A1 __x) {return fabs(__x);}

whereas <cstdlib> defines it as a mere bunch of overloaded functions

inline _LIBCPP_INLINE_VISIBILITY long      abs(     long __x) {return  labs(__x);}
inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) {return llabs(__x);}

I have got a class like_double in my code that has a conversion operator to double. Because of the enable_if, a code such as

like_double x; ... std::abs(x) ...

will fail to compile as the compiler will only consider std::abs(int), std::abs(long), and std::abs(long long) and therefore report ambiguities.

Why is one header using enable_if and not the other one? Is enable_if compulsory at all to abide to the standard here actually?

I encountered that problem as I converted my code to use libc++ instead of libstdc++ 4.2 which used to the default on MacOS and which did not restrict the floating point types.

Best wishes,

Luc Bourhis

More information about the cfe-dev mailing list