Let me start by saying that I understand that performing bit shifts and other bit-wise operations on a floating-point value sounds meaningless and ill-advised. So, moving beyond the "why would you do such a thing" responses...
I have a templated function that is handling some portion of serializing and deserializing various types to/from a binary stream. To "reassemble" multi-byte types from the byte stream, I am making use of bitshifts and bitwise ORs in the usual way. Please assume also that I have endianness and other such concerns under control.
Cutting to it, I'm looking for a clean way within a templated function to reliably cast a variable to an integer of the same size as the argument type (so that I'm allowed to perform bitwise operations on it); where that argument type might be a floating-point or any other arbitrary non-integer type. Pseudo-code example:
uint_same_size_as(U) sameSizeInt;
Where uint_same_size_as(U)
would evaluate to, say, uint32_t
when U
is of type float
, or uint64_t
when U
is of type double
, etc.
Is there any such animal? And please forgive me if this is a basic feature of templates that I'm ignorant of.