std::endian
什么是 std::endian
std::endian 是 C++20 引入的枚举类(scoped enum),定义在 <bit> 头文件中。它用于在编译期检测当前平台的字节序(endianness)。
字节序决定了多字节数据在内存中的存储顺序:
- 小端序(little-endian):低位字节存放在低地址(x86、ARM 默认)
- 大端序(big-endian):高位字节存放在低地址(网络字节序、部分 RISC 处理器)
cpp
#include <bit>
if constexpr (std::endian::native == std::endian::little) {
// 当前平台是小端序
}std::endian 的成员
| 成员 | 说明 |
|---|---|
std::endian::little | 小端序标识 |
std::endian::big | 大端序标识 |
std::endian::native | 当前平台的字节序 |
判断逻辑:
native == little:当前平台是小端序native == big:当前平台是大端序native既不等于little也不等于big:混合字节序(极罕见)
基本示例
cpp
#include <iostream>
#include <bit>
#include <cstdint>
#include <array>
#include <cstring>
#include <algorithm>
#include <iomanip>
// ============================
// 示例1:检测当前平台字节序
// ============================
void detect_demo() {
if constexpr (std::endian::native == std::endian::little) {
std::cout << "当前平台: 小端序 (little-endian)" << std::endl;
} else if constexpr (std::endian::native == std::endian::big) {
std::cout << "当前平台: 大端序 (big-endian)" << std::endl;
} else {
std::cout << "当前平台: 混合字节序" << std::endl;
}
}
// ============================
// 示例2:查看整数在内存中的存储
// ============================
void memory_layout_demo() {
std::uint32_t value = 0x01020304;
// 查看每个字节
auto bytes = std::bit_cast<std::array<std::uint8_t, 4>>(value);
std::cout << "值 0x01020304 在内存中的存储顺序:" << std::endl;
for (int i = 0; i < 4; i++) {
std::cout << " 地址偏移 " << i << ": 0x"
<< std::hex << std::setw(2) << std::setfill('0')
<< (int)bytes[i] << std::dec << std::endl;
}
// 小端序:04 03 02 01(低位在前)
// 大端序:01 02 03 04(高位在前)
}
// ============================
// 示例3:条件编译——根据字节序选择实现
// ============================
// 将 uint32_t 转为网络字节序(大端序)
constexpr std::uint32_t to_network_order(std::uint32_t value) {
if constexpr (std::endian::native == std::endian::big) {
// 大端序平台:不需要转换
return value;
} else {
// 小端序平台:交换字节顺序
return ((value & 0xFF000000) >> 24) |
((value & 0x00FF0000) >> 8) |
((value & 0x0000FF00) << 8) |
((value & 0x000000FF) << 24);
}
}
// 从网络字节序转回主机字节序
constexpr std::uint32_t from_network_order(std::uint32_t value) {
// 转换是对称的
return to_network_order(value);
}
void network_order_demo() {
std::uint32_t host_value = 0x12345678;
std::uint32_t net_value = to_network_order(host_value);
std::uint32_t restored = from_network_order(net_value);
std::cout << "主机序: 0x" << std::hex << host_value << std::endl;
std::cout << "网络序: 0x" << net_value << std::endl;
std::cout << "恢复: 0x" << restored << std::dec << std::endl;
}
// ============================
// 示例4:编译期字节序检查
// ============================
void compile_time_demo() {
// 可以在编译期使用
constexpr bool is_little = (std::endian::native == std::endian::little);
constexpr bool is_big = (std::endian::native == std::endian::big);
std::cout << std::boolalpha;
std::cout << "小端序: " << is_little << std::endl;
std::cout << "大端序: " << is_big << std::endl;
// 在 static_assert 中使用
static_assert(std::endian::native == std::endian::little ||
std::endian::native == std::endian::big,
"不支持混合字节序平台");
}
// ============================
// 示例5:跨平台序列化
// ============================
// 以小端序写入(确保跨平台一致)
void write_little_endian(std::uint32_t value, std::uint8_t* buffer) {
if constexpr (std::endian::native == std::endian::little) {
std::memcpy(buffer, &value, sizeof(value));
} else {
buffer[0] = static_cast<std::uint8_t>(value);
buffer[1] = static_cast<std::uint8_t>(value >> 8);
buffer[2] = static_cast<std::uint8_t>(value >> 16);
buffer[3] = static_cast<std::uint8_t>(value >> 24);
}
}
// 从小端序读取
std::uint32_t read_little_endian(const std::uint8_t* buffer) {
if constexpr (std::endian::native == std::endian::little) {
std::uint32_t value;
std::memcpy(&value, buffer, sizeof(value));
return value;
} else {
return static_cast<std::uint32_t>(buffer[0]) |
(static_cast<std::uint32_t>(buffer[1]) << 8) |
(static_cast<std::uint32_t>(buffer[2]) << 16) |
(static_cast<std::uint32_t>(buffer[3]) << 24);
}
}
void serialization_demo() {
std::uint8_t buffer[4];
std::uint32_t original = 0xDEADBEEF;
write_little_endian(original, buffer);
std::cout << "序列化后字节: ";
for (auto b : buffer) {
std::cout << std::hex << std::setw(2) << std::setfill('0')
<< (int)b << " ";
}
std::cout << std::dec << std::endl;
auto restored = read_little_endian(buffer);
std::cout << "反序列化: 0x" << std::hex << restored << std::dec << std::endl;
}
// ============================
// 示例6:模板中根据字节序特化
// ============================
template<std::endian E>
struct EndianName {
static constexpr const char* value = "unknown";
};
template<>
struct EndianName<std::endian::little> {
static constexpr const char* value = "little-endian";
};
template<>
struct EndianName<std::endian::big> {
static constexpr const char* value = "big-endian";
};
void template_demo() {
std::cout << "平台字节序: "
<< EndianName<std::endian::native>::value << std::endl;
}
int main() {
std::cout << "=== 检测字节序 ===" << std::endl;
detect_demo();
std::cout << "\n=== 内存布局 ===" << std::endl;
memory_layout_demo();
std::cout << "\n=== 网络字节序 ===" << std::endl;
network_order_demo();
std::cout << "\n=== 编译期检查 ===" << std::endl;
compile_time_demo();
std::cout << "\n=== 序列化 ===" << std::endl;
serialization_demo();
std::cout << "\n=== 模板特化 ===" << std::endl;
template_demo();
return 0;
}C++20 之前的字节序检测方式对比
| 方式 | 标准 | 编译期 | 可移植 |
|---|---|---|---|
std::endian | C++20 | 是 | 是 |
__BYTE_ORDER__ 宏 | GCC/Clang 扩展 | 是 | 否 |
| 运行时 union 检测 | 所有版本 | 否 | 是 |
htonl 比较 | POSIX | 否 | 部分 |
适用场景
- 网络编程中的字节序转换
- 跨平台序列化/反序列化
- 二进制文件格式的读写
- 根据字节序选择优化路径
- 与硬件寄存器交互
常见问题
x86/x64 平台是什么字节序?
x86 和 x64(包括 Intel 和 AMD)都是小端序。ARM 默认也是小端序,但可以配置为大端序。
std::endian 可以在 constexpr 和 if constexpr 中使用吗?
可以。这正是它的主要用途——在编译期根据平台字节序选择不同的代码路径,另一条路径会被编译器完全移除。
混合字节序是什么?
极少数处理器(如某些旧的 ARM 配置)会对不同大小的数据使用不同的字节序。这种情况下 native 既不等于 little 也不等于 big。
注意事项
- std::endian 是枚举类,不是函数
- 使用
if constexpr而不是普通if来在编译期选择分支 - 网络协议通常使用大端序(网络字节序)
- 大多数现代桌面/移动平台都是小端序
- 需要
<bit>头文件
总结
std::endian 是 C++20 提供的标准化字节序检测工具。通过比较 std::endian::native 与 std::endian::little 或 std::endian::big,可以在编译期确定当前平台的字节序。它替代了以前依赖编译器扩展或运行时检测的做法,让跨平台的字节序处理代码更简洁、可移植。配合 if constexpr 使用,编译器可以在编译期选择正确的代码路径并移除无用分支。