std::endian - cppreference.com

From cppreference.com

Defined in header

<bit>

enumclassendian{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::native equals std::endian::little.

If all scalar types are big-endian, std::endian::native equals std::endian::big.

Corner case platforms are also supported:

If all scalar types have sizeof equal to 1, endianness does not matter and all three values, std::endian::little, std::endian::big, and std::endian::native are the same.

If the platform uses mixed endian, std::endian::native equals neither std::endian::big nor std::endian::little.

Possible implementation

enumclassendian{#if defined(_MSC_VER) && !defined(__clang__)little=0,big=1,native=little#elselittle=__ORDER_LITTLE_ENDIAN__,big=__ORDER_BIG_ENDIAN__,native=__BYTE_ORDER__#endif};Notes

Feature-test

macroValueStdFeature

__cpp_lib_endian

201907L

(C++20)std::endianExample

Run this code

#include<bit>#include<iostream>intmain(){ifconstexpr(std::endian::native==std::endian::big)std::cout<<"big-endian\n";elseifconstexpr(std::endian::native==std::endian::little)std::cout<<"little-endian\n";elsestd::cout<<"mixed-endian\n";}Possible output:

mixed-endian See also