Defined in header <bit> | ||
---|---|---|
enum class endian { little = /*implementation-defined*/, big = /*implementation-defined*/, native = /*implementation-defined*/ }; | (since C++20) |
Indicates the endianness of all scalar types:
std::endian::native
equals std::endian::little
std::endian::native
equals std::endian::big
Corner case platforms are also supported:
std::endian::little
, std::endian::big
, and std::endian::native
are the same. std::endian::native
equals neither std::endian::big
nor std::endian::little
. enum class endian { #ifdef _WIN32 little = 0, big = 1, native = little #else little = __ORDER_LITTLE_ENDIAN__, big = __ORDER_BIG_ENDIAN__, native = __BYTE_ORDER__ #endif };
Feature-test macro |
---|
__cpp_lib_endian |
#include <bit> #include <iostream> int main() { if constexpr (std::endian::native == std::endian::big) std::cout << "big-endian\n"; else if constexpr (std::endian::native == std::endian::little) std::cout << "little-endian\n"; else std::cout << "mixed-endian\n"; }
Possible output:
little-endian
(C++23) | reverses the bytes in the given integer value (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/endian