std::endian
From cppreference.com
| 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:
- If all scalar types are little-endian,
std::endian::nativeequalsstd::endian::little. - If all scalar types are big-endian,
std::endian::nativeequalsstd::endian::big.
Corner case platforms are also supported:
- If all scalar types have
sizeofequal to1, endianness does not matter and all three values,std::endian::little,std::endian::big, andstd::endian::nativeare the same. - If the platform uses mixed endian,
std::endian::nativeequals neitherstd::endian::bignorstd::endian::little.
Possible implementation
enum class endian
{
#if defined(_MSC_VER) && !defined(__clang__)
little = 0,
big = 1,
native = little
#else
little = __ORDER_LITTLE_ENDIAN__,
big = __ORDER_BIG_ENDIAN__,
native = __BYTE_ORDER__
#endif
};
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_endian |
201907L |
(C++20) | std::endian
|
Example
Run this code
#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:
mixed-endian
See also
(C++23) |
reverses the bytes in the given integer value (function template) |
C documentation for endian
| |