2 ms·
Yes, there is also std::endian::{big, small, native} that can be used to check if you need to byteswap or not. So a generic hton* is just: auto to_network_e
by gpderetta 4y ago
Yes, there is also std::endian::{big, small, native} that can be used to check if you need to byteswap or not. So a generic hton* is just:
auto to_network_endian(auto std::integral value) {
if constexpr (std::endian::native != std::endian::big) {
return std::byteswap(value);
} else {
return value;
}
}
ntoh is left as an exercise :).
edit: in practice I think std::endian and std::byteswap is a compromise between those that wanted a simple {to,from}_network_endian and those that wanted strongly typed wrappers to prevent mixing object with distinct endianess (a-la boost::endian). As the commitee couldn't reach consensus, this is the compromise and you can build your own thing with these portable bits.