microsoft / STL

MSVC's implementation of the C++ Standard Library.
Other
9.88k stars 1.45k forks source link

<cmath>: can't pass integer types to `std::isnan` #4747

Closed zygoloid closed 2 days ago

zygoloid commented 3 days ago

Describe the bug

std::isnan fails to compile when passed an argument of integer type.

Command-line test case

#include <cmath>
bool b = std::isnan(0);

results in:

/opt/compiler-explorer/windows/10.0.10240.0/ucrt/math.h(403): error C2668: 'fpclassify': ambiguous call to overloaded function
/opt/compiler-explorer/windows/10.0.10240.0/ucrt/math.h(288): note: could be 'int fpclassify(long double) noexcept'
/opt/compiler-explorer/windows/10.0.10240.0/ucrt/math.h(283): note: or       'int fpclassify(double) noexcept'
/opt/compiler-explorer/windows/10.0.10240.0/ucrt/math.h(278): note: or       'int fpclassify(float) noexcept'
/opt/compiler-explorer/windows/10.0.10240.0/ucrt/math.h(403): note: while trying to match the argument list '(int)'
<source>(3): note: see reference to function template instantiation 'bool isnan<int>(_Ty) noexcept' being compiled
        with
        [
            _Ty=int
        ]
Compiler returned: 2

Live on compiler explorer

Expected behavior

<cmath> is required to provide sufficient additional overloads such that std::isnan can be applied to integer types, with the behavior that the integer value is implicitly converted to double (so that std::isnan(0) returns false). This doesn't appear to work under MSVC.

zygoloid commented 3 days ago

Relevant standard rule

frederick-vs-ja commented 3 days ago

Duplicate of #519. Other duplicates: #908, #3400.

This is (partially) fixed by #4537. The remain issue, additional overloads missing in <math.h> in C++ mode, is out of the scope of this repo. I opened DevCom-10294165 but it was recently closed as Lower Priority...

StephanTLavavej commented 2 days ago

Yep, @frederick-vs-ja is correct, this is fixed for <cmath>. #4537 shipped in VS 2022 17.11 Preview 2 which is available now. (Compiler Explorer got actual MSVC compilation and execution back :tada: but is currently limited to 17.8).