'Compile issue with MSVC std::isfinite

Hi the following codes won't compile in MSCV; it gives error

(9): error C3615: constexpr function 'Test' cannot result in a constant expression due to the use of std::isfinite!?
constexpr int Test(double value)
{
    if (!std::isfinite((double)value))
    {
        return 1;
    }

    return 0;
}

//}

gcc is fine, how to fix it?



Solution 1:[1]

std::isfinite isn't marked as constexpr until C++23. Apparently gcc's standard library has already implemented this feature and MSVC's hasn't yet.

Make sure, when you compile with MSVC, to ask for the latest version of the standard with /std:c++latest. But I'm guessing they just haven't made that change yet.

In the meantime, you could write your own implementation of isfinite that is constexpr. I think the Wikipedia page on IEEE floating point has enough detail to come up with the appropriate test condition. I think the infinities and NaN values have all bits of the exponent field set to 1.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian McCarthy