C++ <cstdint>
详解 C++11 固定宽度整数头文件 <cstdint>:精确宽度 intN_t/uintN_t、最小宽度 int_leastN_t、最快宽度 int_fastN_t、最大宽度 intmax_t、指针整数 intptr_t 及边界值宏与跨平台注意事项。
学完这一章,你能够
- 详解 C++11 固定宽度整数头文件 <cstdint>:精确宽度 intN_t/uintN_t、最小宽度 int_leastN_t、最快宽度 int_fastN_t、最大宽度 intmax_t、指针整数 intptr_t 及边界值宏与跨平台注意事项。
跨平台宽度问题与 <cstdint> 固定宽度类型
在传统 C++ 中,内置整数类型(如 `int`、`long`)的位宽仅受标准最低位数约束,实际尺寸随平台与数据模型变化(例如 `long` 在 Linux x86-64 LP64 下为 64 位,在 Windows x64 LLP64 下仅为 32 位)。为解决网络协议、二进制文件格式与跨平台序列化中的位宽不确定性,C++11 引入 `<cstdint>`(对应 C99 `<stdint.h>`),提供一组具有明确位宽语义的整数类型别名与边界常量宏。
最常用的精确宽度(定宽)类型包括有符号 `int8_t`、`int16_t`、`int32_t`、`int64_t` 与无符号 `uint8_t`、`uint16_t`、`uint32_t`、`uint64_t`(位于 `std::` 命名空间,主流实现通常也同步暴露在全局命名空间)。使用这些类型时有两点关键细节:其一,`int8_t`/`uint8_t` 底层通常是 `signed char`/`unsigned char` 的别名,直接送入 `std::cout` 会被当作 ASCII 字符打印,需用 `static_cast<int>` 或 `static_cast<unsigned int>` 转换后再输出数值;其二,书写 64 位大字面量时应使用 `LL`/`ULL` 后缀(或 `INT64_C`/`UINT64_C` 宏),且注意 `-2147483648` 在标准下常以 `-2147483647 - 1` 表示以避免字面量溢出。
最小宽度、最快宽度与特殊宽度类型
严格精确宽度类型 `intN_t`/`uintN_t` 在 C++ 标准中属于可选类型(仅在目标平台存在精确无填充位的补码整数时提供)。若代码只需“至少 `N` 位”的强可移植保证,标准强制提供的**最小宽度类型**(`int_least8_t`/`16`/`32`/`64` 及其 `uint_leastN_t` 版本)会选择满足位宽下限的最紧凑整数;若优先考虑 CPU 寄存器访问速度,**最快宽度类型**(`int_fast8_t`/`16`/`32`/`64` 及其 `uint_fastN_t` 版本)会选择平台运算速度最快的至少 `N` 位整数。
`<cstdint>` 还定义了两类特殊用途整数:最大宽度整数 `std::intmax_t` 与 `std::uintmax_t`(分别对应平台支持的最大有符号/无符号整数,用于通用整数格式与范围运算);以及可选的指针整数 `std::intptr_t` 与 `std::uintptr_t`(足以无损保存 `void*` 指针值,适合在底层地址对齐、哈希或标签指针运算中配合 `reinterpret_cast` 使用)。
边界值宏与跨平台工程实践
为配合定宽类型进行溢出防护与边界检查,`<cstdint>` 提供了全套极值宏:`INT8_MIN`(-128)、`INT8_MAX`(127)、`UINT8_MAX`(255),`INT16_MIN`/`INT16_MAX`/`UINT16_MAX`,`INT32_MIN`/`INT32_MAX`/`UINT32_MAX`,`INT64_MIN`/`INT64_MAX`/`UINT64_MAX`,以及 `INTMAX_MAX`/`UINTMAX_MAX` 等(同样也可在泛型代码中结合 `<limits>` 的 `std::numeric_limits<std::uint32_t>::max()` 使用)。在编写跨平台协议、嵌入式驱动或高性能数值代码时,合理选用 `<cstdint>` 能显著提升可预测性与可移植性。
实测 <cstdint> 精确/最小/最快宽度类型、intptr_t 指针往返与全部边界宏
展开查看收起
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: Exact-width integer sizes and boundary values
std::int8_t a = -128;
std::uint8_t b = 255;
std::int16_t c = -32768;
std::uint16_t d = 65535;
std::int32_t e = -2147483647 - 1;
std::uint32_t f = 4294967295U;
std::int64_t g = -9223372036854775807LL;
std::uint64_t h = 18446744073709551615ULL;
bool c1 =
sizeof(a) == 1 && sizeof(b) == 1 &&
sizeof(c) == 2 && sizeof(d) == 2 &&
sizeof(e) == 4 && sizeof(f) == 4 &&
sizeof(g) == 8 && sizeof(h) == 8 &&
static_cast<int>(a) == -128 &&
static_cast<unsigned int>(b) == 255U &&
c == -32768 && d == 65535 &&
e == INT32_MIN && f == UINT32_MAX &&
g == -9223372036854775807LL && h == UINT64_MAX;
cases.push_back({"exact_width_sizes_and_values", lab::jbool(true), lab::jbool(c1), c1});
// Case 2: Least-width and fast-width integer guarantees
std::int_least8_t l8 = -128;
std::uint_least8_t ul8 = 255;
std::int_least32_t l32 = 2147483647;
std::uint_least64_t ul64 = 18446744073709551615ULL;
std::int_fast8_t f8 = -128;
std::uint_fast8_t uf8 = 255;
std::int_fast32_t f32 = 2147483647;
std::uint_fast64_t uf64 = 18446744073709551615ULL;
bool c2 =
sizeof(std::int_least8_t) >= 1 && sizeof(std::int_least16_t) >= 2 &&
sizeof(std::int_least32_t) >= 4 && sizeof(std::int_least64_t) >= 8 &&
sizeof(std::int_fast8_t) >= 1 && sizeof(std::int_fast16_t) >= 2 &&
sizeof(std::int_fast32_t) >= 4 && sizeof(std::int_fast64_t) >= 8 &&
l8 == -128 && ul8 == 255 && l32 == 2147483647 && ul64 == UINT64_MAX &&
f8 == -128 && uf8 == 255 && f32 == 2147483647 && uf64 == UINT64_MAX;
cases.push_back({"least_and_fast_width_types", lab::jbool(true), lab::jbool(c2), c2});
// Case 3: intmax_t / uintmax_t and intptr_t / uintptr_t pointer round-trip
std::intmax_t max_i = INTMAX_MAX;
std::uintmax_t max_u = UINTMAX_MAX;
int target = 2026;
std::uintptr_t addr_u = reinterpret_cast<std::uintptr_t>(&target);
std::intptr_t addr_s = reinterpret_cast<std::intptr_t>(&target);
int* restored_u = reinterpret_cast<int*>(addr_u);
int* restored_s = reinterpret_cast<int*>(addr_s);
bool c3 =
sizeof(std::intmax_t) >= 8 && sizeof(std::uintmax_t) >= 8 &&
max_i >= INT64_MAX && max_u >= UINT64_MAX &&
restored_u == &target && restored_s == &target && *restored_u == 2026;
cases.push_back({"intmax_and_intptr_roundtrip", lab::jbool(true), lab::jbool(c3), c3});
// Case 4: Limit macros verification
bool c4 =
INT8_MIN == -128 && INT8_MAX == 127 && UINT8_MAX == 255 &&
INT16_MIN == -32768 && INT16_MAX == 32767 && UINT16_MAX == 65535 &&
INT32_MIN == (-2147483647 - 1) && INT32_MAX == 2147483647 && UINT32_MAX == 4294967295U &&
INT64_MIN == (-9223372036854775807LL - 1) && INT64_MAX == 9223372036854775807LL &&
UINT64_MAX == 18446744073709551615ULL;
cases.push_back({"limit_macros_verification", lab::jbool(true), lab::jbool(c4), c4});
lab::emit(cases);
return 0;
}
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
因为 `std::uint8_t` 通常是 `unsigned char` 的类型别名,`operator<<` 对字符类型重载按 ASCII 字符输出;将其转换为 `static_cast<unsigned int>(a)`(或一元 `+a`)即可按整数打印。