stdlib.h abs(long) overload?

Stephan Bergmann sbergman@redhat.com
Mon Mar 3 16:40:00 GMT 2014


I discovered that at least with Fedora 20 combination of libstdc++ 4.8.2 
and glibc 2.18, including stdlib.h does not make available an abs(long) 
overload to abs(int).  That is, in a 64 bit environment,

> #include <iostream>
> #include <stdlib.h>
> int main() {
>     long n1 = -4294967296;
>     long n2 = abs(n1);
>     std::cout << n1 << " " << n2 << "\n";
> }

prints

> -4294967296 0

instead of

> -4294967296 4294967296

It works correctly when using cstdlib and std::abs instead, and my 
reading of the C++ standards (both 03 and 11) is that including stdlib.h 
should make the overload available in the global namespace.

The __CORRECT_ISO_CPP_STDLIB_H_PROTO in the source (apparently only used 
for Solaris) makes me assume that this is a known issue.  Is the take of 
libstdc++ on this that it is a bug in glibc that needs to be fixed 
there?  (Not sure whether the alternative of working around the glibc 
shortcoming in libstdc++, by providing a stdlib.h wrapper, say, would be 
feasible at all.)

Clarification welcome,
Stephan



More information about the Libstdc++ mailing list