Is there a way to get the bits of a double as an int64_t in a constexpr function in C++?
I first tried the usual *reinterpret_cast<int64_t*>(&d)
, but that's not allowed in constexpr code, which makes some sense. Then, I tried the usual fallback, (which usually results in slower code, unfortunately for the cases where it's run at runtime, instead of compile time), of creating a struct with a union of double and int64_t, with a constexpr constructor that takes a double to initialize the double member. The constructor is accepted, and I've had other cases where constexpr code is okay with unions in structs, but if any code tries to read the int64_t member of the union, Visual Studio 2017 complains:
error C2131: expression did not evaluate to a constant
note: failure was caused by accessing a non-active member of a union
note: see usage of 'big_float::DoubleInt::i'
GCC complains:
error: accessing 'DoubleInt::<unnamed union>::i' member instead of initialized 'DoubleInt::<unnamed union>::d' member in constant expression
It's rather unfortunate to not be able to do this, particularly since I'm implementing a high-precision floating-point class, requiring access to the bits, and I'd like almost all of the functions to be able to run at compile-time if applicable. Does anyone have any workarounds that won't result in extremely slow runtime performance for the runtime case?
(P.S. Please don't ignore the question and only reply with something snarky like "the standard doesn't guarantee that double is 64 bits or that int64_t is 64 bits", because I really don't care about things that never happen. There seems to be an awful lot of that sort of snark on StackOverflow. Every compiler I care about on every platform I care about will always have that guarantee, else I'll stop using them. I would appreciate actual help for actual code, please. Thanks.)