Rationale for cmath's std::abs(short) -> double

Stephan Bergmann sbergman@redhat.com
Wed Feb 3 10:25:00 GMT 2016

I once again wonder why 
<https://gcc.gnu.org/viewcvs/gcc?view=revision&revision=150022> added that

> template<typename _Tp> typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, double>::__type abs(_Tp)

overload to cmath, causing a call to std::abs with an argument of type 
short to return a double, even if cstdlib is also included (which 
declares the int, long, long long overloads).  That commit does not give 
any rationale.

(I apparently wondered about that already a couple of years ago, but all 
I can find now in my notes is "[...] only to discover libstdc++'s 
over-eager interpretation of C++11 cmath requirements causes std::abs 
with a short argument to chose the double overload," which no longer 
rings much of a bell with me.)

This issue happens to crop up again now in the context of the 
LibreOffice code base, see <https://gerrit.libreoffice.org/#/c/22050/> 
"Make abs return int, not double," which reportedly is on Fedora Rawhide.

More information about the Libstdc++ mailing list