An integral type cannot be NaN from my understanding, so std::isnan
can just return false
when given an integral type. Why does arg
need to be cast to double
as it states here
1 Answers
It says "equivalent to the behavior if it's cast to double
". Which could mean "since literally every value of the given type can produce a valid (if possibly imprecise) double
value, the implementation is just a compile time return false;
". Lots of C++ has "here's how this behaves logically", and then relies on the as-if rule to say "but you can do something else that's logically equivalent if, aside from performance, the behavior is the same when done some other way."
Still useful to have it, for when you have a templated class/function that is realized on multiple numeric types, but the templated type might or might not have a concept of NaN. You can unconditionally filter (or otherwise handle) NaNs, and, with optimizations enabled at least, it's nigh guaranteed that the path for isnan(someinteger)
will get compiled out, but when the template realized for floating point types, it will actually perform the test and branch.

- 143,180
- 12
- 188
- 271