While searching for a way to combine sizeof(double)
char
s to a double
, I read in several posts, that using std::memcpy
was the recommended way:
char bytes[sizeof(double)];
// fill array
double d;
std::memcpy(&d, bytes, sizeof(double));
However, I wonder why further usage of d
can be defined behavior.
If it was not a double
, but a complex class object, accessing it surely would not be defined either, would it? So, why should it be the case for a double
.
Edit: To make my problem clear, I wanna specify my goal: I would like to find a way to combine several char
s to a double
and further use this double, without causing undefined behavior. I do not expect the value of the double
to be specified. I deem this impossible anyway, since the standard does not even say anything about the size, not to mention bit layout of double
. However, I demand d
to have some valid (i. e. 'accessible') double
-value.