You can use std::endian
if you have access to C++20 compiler such as GCC 8+ or Clang 7+.
Note: std::endian
began in <type_traits>
but was moved to <bit>
at 2019 Cologne meeting. GCC 8, Clang 7, 8 and 9 have it in <type_traits>
while GCC 9+ and Clang 10+ have it in <bit>
.
#include <bit>
if constexpr (std::endian::native == std::endian::big)
{
// Big endian system
}
else if constexpr (std::endian::native == std::endian::little)
{
// Little endian system
}
else
{
// Something else
}