I accidentally fed a default initializeable struct
to std::numeric_limits<somestruct>::infinity()
. What I got back was a default struct.
Why does the standard allow this to compile and return such an unexpected value?
#include <iostream>
struct somestruct {
uint64_t a = 7;
};
inline ::std::ostream& operator <<(::std::ostream& s, const somestruct& q) {
s << q.a;
return s;
}
int main(int argc, char **argv) {
constexpr const auto inf = ::std::numeric_limits<somestruct>::infinity();
std::cout << inf << std::endl;
return 0;
}
Godbolt for compilation verification
"Why" questions are notoriously unanswerable, but the direct answer is: that's how it's specified:
namespace std { template<class T> class numeric_limits { // ... static constexpr T infinity() noexcept { return T(); } // ... }; }
With the extra text that infinity()
is:
Meaningful for all specializations for which
has_infinity != false
In your case, numeric_limits<somestruct>::has_infinity
is false
, so infinity()
isn't meaningful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With