简体   繁体   中英

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).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM