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

Howard Hinnant hhinnant at apple.com
Thu May 3 08:00:46 PDT 2012

On May 3, 2012, at 10:37 AM, Luc Bourhis wrote:

> Hi,
> 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.

I've committed a fix in revision 156064.

Most of the cmath functions remain templates in order to handle integral arguments.  However since integral arguments for abs are already handled in <cstdlib> we do not need to follow that design pattern with abs.

Thanks for your report.


More information about the cfe-dev mailing list