Search code examples
c++mingwstd

Using #include <cmath>, why it always select the long version of abs instead of choose due to the input type?


I've noticed this strange thing on an app I'm using (trying to recreate here, not sure if I'm able).

Basically, if I include #include <cmath> and I call abs() function (without namespace), whatever values I pass to it (double, float, and so on) it seems to evaluate the int version of it:

enter image description here

If instead I specify std::abs(), it correctly takes the right method (double in this case):

enter image description here

Why this? Isn't abs() the same of std::abs() including #include <cmath>? Which function does it using "bypassing double" and always selecting the int version?


Solution

  • You're getting that result because it's allowed by the Standard. If you want ::abs, include <stdlib.h>. If you want std::abs, include <cmath>.

    In Standard C++, including one standard header may cause other headers to be included, but implementations vary in their choices (even between releases of the same implementation). And including <cmath> may put the C++ overloads in the global namespace. Again, implementations differ. For portable code, stick to what the standard says.