[Bug c++/78042] g++ does not select the corresponding overloads of abs() for long and long long

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Oct 20 10:38:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78042

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Dominik Muth from comment #0)
> Obviously int abs(int) is called here all the time. However I expect the
> corresponding overloads to be selected according to
> http://en.cppreference.com/w/cpp/numeric/math/abs.

Your expectation is wrong.

If you include <cstdlib> then you get all the required overloads in namespace
std (and maybe, unportably, some or all in the global namespace as well).

If you include <stdlib.h> then you get all the required overloads in the global
namespace (and maybe, unportably, some or all in namespace std as well).


So if you include <cstdlib> and call ::abs then your code is not portable or
reliable and doesn't behave as specified by the C++ standard. If you call
std::abs (either explicitly, or after "using std::abs" or "using namespace
std") then it would work, or if you included <stdlib.h> then it would work.


More information about the Gcc-bugs mailing list