double をシリアライズするときとかは、 std::numeric_limit< double >::is_iec559 を assert しておけばいいのかなー。
#include <cassert> #include <limits> #include <boost/cstdint.hpp> template<class Itr> void serialize(Itr it, const double d) { // ↓ こんな感じ assert(sizeof(double) == sizeof(boost::uint64_t)); // 64 bit assert(std::numeric_limits< double >::is_iec559); // IEEE 754 union { double d; boost::uint64_t u; } du; du.d = d; boost::uint64_t u = du.u; *it++ = static_cast<unsigned char>(u & 0xff); *it++ = static_cast<unsigned char>((u>>>8) & 0xff); *it++ = static_cast<unsigned char>((u>>>16) & 0xff); *it++ = static_cast<unsigned char>((u>>>24) & 0xff); *it++ = static_cast<unsigned char>((u>>>32) & 0xff); *it++ = static_cast<unsigned char>((u>>>40) & 0xff); *it++ = static_cast<unsigned char>((u>>>48) & 0xff); *it++ = static_cast<unsigned char>((u>>>56) & 0xff); }