|
Boost Users : |
Subject: Re: [Boost-users] Convert big endian to little endian (floating point values)
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2015-12-11 14:54:34
> Reading from an inactive member of a union is plain and simply undefined
> behavior. To read and write the bits of the `float` in a well defined
> way use `std::memcpy`.
Curiously, it is undefined in C++, whereas well-defined in C:
"6.5.2.3 Structure and union members
If the member used to read the contents of a union object is not the
same as the member last used to store a value in the object, the
appropriate part of the object representation of the value is
reinterpreted as an object representation in the new type as described
in 6.2.6 (a process sometimes called type punning). This might be a
trap representation."
Since C++ includes C as a normative reference it would be a good idea to
allow it also in C++. memcpy is fine if the compiler optimizes it as an
intrinsic, but inefficient in some implementations to copy only a couple
of bytes.
Best,
Ion
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net