diff --git a/boost/endian/conversion2.hpp b/boost/endian/conversion2.hpp index 9137374..1e4d9c7 100644 --- a/boost/endian/conversion2.hpp +++ b/boost/endian/conversion2.hpp @@ -21,9 +21,10 @@ // -- reorder implementation approach suggested by tymofey, with avoidance of // undefined behavior as suggested by Giovanni Piero Deretta, and a further // refinement suggested by Pyry Jahkola. -// -// Q: Why no general reorder template? A: It wouldn't work for classes with more than -// one member; each member must be reordered individually. +// -- general reorder function template to meet requests for UDT support by +// Vicente Botet and others. +// -- general reorder function template implementation approach using std::reverse +// suggested by Mathias Gaunard // //--------------------------------------------------------------------------------------// @@ -44,11 +45,16 @@ namespace endian2 // TODO: Need implementation // TODO: Need to verify the return does not invoke undefined behavior (as might happen - // if there are unutterable floating point values, similar to the unutterable pointer - // values on some architectures + // if there are unutterable floating point values, such as happens with the unutterable + // pointer values on some architectures inline float reorder(float x); inline double reorder(double x); + // TODO: Would pass by value be better for the following functions? + + template + inline T reorder(const T& x); + template inline T big(const T& x); // Return: x if native endianness is big, otherwise reorder(x); @@ -109,6 +115,18 @@ namespace endian2 | (step16 & 0xFF00FF00FF00FF00) >> 8; } + + template + inline T reorder(const T& x) + { + T tmp; + std::reverse( + reinterpret_cast(&x), + reinterpret_cast(&x) + sizeof(T), + reinterpret_cast(&tmp)); + return tmp; + } + template inline T big(const T& x) {