CKTIS / C++
std::endian — byte order
std::endian from <bit> describes the byte order of scalar types in the execution environment. Compare native with little and big, while also handling the special cases.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <bit>
std::endian::native
std::endian::little
std::endian::bigstd::endian is a scoped enumeration, enum class, from <bit>. std::endian::little denotes least-significant-byte-first order, while std::endian::big denotes most-significant-byte-first order, with increasing memory addresses.
std::endian::native describes the execution environment: it equals std::endian::little when all scalar types are little-endian, or std::endian::big when all are big-endian. Otherwise, it can equal neither value. Therefore, do not assume that not matching little means big.
If all scalar types occupy one byte, all three values are equal. Otherwise, little and big are distinct. The example checks this special case first, then selects the appropriate branch using if constexpr.
This is representation information, not a byte-reordering operation or a complete serialization facility. A repository's C++23 compiler setting does not establish the properties of its production execution environment.
Examples
#include <bit>
#include <iostream>
int main() {
if constexpr (std::endian::little == std::endian::big) {
std::cout << "all scalar types occupy one byte\n";
} else if constexpr (std::endian::native == std::endian::little) {
std::cout << "little-endian\n";
} else if constexpr (std::endian::native == std::endian::big) {
std::cout << "big-endian\n";
} else {
std::cout << "neither uniform little nor uniform big\n";
}
}
Result
little-endian
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.