How can I determine at compile time if my platform is little endian or big endian? I have seen many ways to determine at runtime using casting, and some platform-dependent options. Is there a portable or standard way to do this?
constexpr bool is_little_endian = ?;
C++20 added std::endian
to <bit>
* which can be used in a constexpr context.
Live example of below code:
if constexpr (std::endian::native == std::endian::little) {
std::cout << "litle endian\n";
} else if constexpr(std::endian::native == std::endian::big) {
std::cout << "big endian\n";
} else {
std::cout << "something silly\n";
}
* It was originally <type_traits>
and will be there in older implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With