0

While porting a program from Visual Studio to GCC, I bumped in a discrepancy in behavior related to global namespace abs function (not std::abs), which after code minimization can be shown in the following example:

#include <iostream>

int main() {
    std::cout << abs( -0.5 );
}

Here Visual Studio prints 0.5, but GCC just 0. It seems that only integer valued abs function is present in global namespace of GCC's libstdc++. Clang with libc++ behaves like Visual Studio printing 0.5. Online demo: https://gcc.godbolt.org/z/EWbW5sf7c

Do MSVC and Clang extend the standard defining more functions in the global namespace than required?

P.S. I can find some old related questions, e.g. Ambiguous overload call to abs(double), but the answer there speculates that the code as above ought not to compile at all.

Fedor
  • 17,146
  • 13
  • 40
  • 131
  • `abs` is not provided in ``. Try changing `abs` to `std::abs`, and providing `#include `. – Eljay Apr 08 '23 at 11:34
  • @Eljay, thanks. `abs` is indirectly included via `` in all implementations, adding `#include ` does not change anything in this example: https://gcc.godbolt.org/z/Yrac8rExd. I agree that using `std::abs` is the only safe way, but unfortunately there are large codebases that do use simple `abs` for floating-point arguments. – Fedor Apr 09 '23 at 07:04
  • *Do MSVC and Clang extend the standard defining more functions in the global namespace than required?* To answer your question: **no**, their defining more functions in the global namespace does not extend the standard. – Eljay Apr 09 '23 at 11:55

0 Answers0