Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::isnan not constexpr?

As of C++20 std::isnan is not constexpr.

Is there a technical reason for this?

All I can think of is that it is in "C header" so nobody bothered to fix it (<limits> functions dealing with floats are constexpr, but unfortunately numeric_limits does not contain isnan check).

like image 303
NoSenseEtAl Avatar asked Jul 09 '20 14:07

NoSenseEtAl


1 Answers

The answer is basically "Because the proposal hasn't been merged into the standard yet"

P0533 Proposes what you are asking, and it did not make it into C++20.

One of the holdups is that perhaps this proposal is a little big... some of the functions in <cmath> or <cstdlib> are allowed to set global error codes, which complicates the whole "constexpr all the things" proposal*

I don't think there should be any issue with these classification functions; std::isnan and its sibling std::fpclassify are defined to behave identically to the C macros isnan and fpclassify respectively. This will need to change slightly.


*(aside: p2043 "Don't constexpr all the things" is tangentially relevant here, but it's only talking about metaprogramming)

like image 98
AndyG Avatar answered Nov 08 '22 18:11

AndyG