Andrey Semashev wrote:
IMHO, if you need something more than a byte swap then you need a different library.
You're just repeating what you said. My answer remains the same. You never need a byte swap. What you need is always something else, to which a byte swap is sometimes the answer. Specifically, what you need is to be able to read or write integers or floats in non-native formats.
Well, in some of the projects I work on we have something as simple as:
void write_be32(std::uint32_t n, void* p); std::uint32_t read_be32(const void* p); // etc.
template< typename T, std::size_t Size = sizeof(T) > struct big_endian;
// Specializations for different values of Size template< typename T > struct big_endian< T, 4 > { static void write(T n, void* p) { write_be32(n, p); }
That's more or less what I have, too. Where do we disagree? Note that
void write_be32(std::uint32_t n, void* p);
needs not assume presence of uint32_t or a particular representation of n. Given any integer n in any representation, it could still portably write a big-endian 32 bit integer into p. void write_be32( uint_least32_t n, unsigned char p[4] ) { // I assume CHAR_BIT of 8 here /* assert( n < 2^32 ); */ p[0] = ( n >> 24 ) & 0xFF; p[1] = ( n >> 16 ) & 0xFF; p[2] = ( n >> 8 ) & 0xFF; p[3] = n & 0xFF; } This is, however, not what Boost.Endian does.