diff --git a/endian.hpp b/endian.hpp index 13d0050e..2b5929fc 100644 --- a/endian.hpp +++ b/endian.hpp @@ -88,17 +88,22 @@ struct from_endian { static_assert (std::is_integral::value || std::is_enum::value, "endian conversion is only defined for integrals currently"); - auto u = static_cast< - typename std::conditional< - std::is_signed::value, - typename sized_type::sint, - typename sized_type::uint - >::type - > (v); + union { + typename sized_type::sint sint; + typename sized_type::uint uint; + }; - return static_cast ( - (src == endian::LITTLE) ? ltoh (u) : btoh (u) - ); + if (std::is_signed::value) + sint = v; + else + uint = v; + + uint = (src == endian::LITTLE) ? ltoh (uint) : btoh (uint); + + if (std::is_signed::value) + return T (sint); + else + return T (uint); } endian src;