How to write constexpr swap function to change endianess of an integer?
问题 How to write a constexpr function to swap endianess of an integer, without relying on compiler extensions and can you give an example on how to do it? 回答1: Yes, it's pretty easy; here's a recursive (C++11-compatible) implementation (unsigned integral types only): #include <climits> #include <cstdint> #include <type_traits> template<class T> constexpr typename std::enable_if<std::is_unsigned<T>::value, T>::type bswap(T i, T j = 0u, std::size_t n = 0u) { return n == sizeof(T) ? j : bswap<T>(i >