fmt是C++20标准std::format的参考实现和超集,更安全高效;支持编译时检查、自定义类型、日期时间等;可通过vcpkg/Conan安装或单头文件嵌入。

fmt 是一个现代、安全、高性能的 C++ 格式化库,比 std::iostream 更简洁,比 printf 更类型安全。它已被纳入 C++20 标准(std::format),而 fmt 库本身是其参考实现和超集,支持更多功能(如编译时格式检查、自定义类型格式化、日期时间、宽字符等)。
快速上手:安装与基础用法
推荐使用 vcpkg 或 Conan 管理依赖,也可直接下载单头文件 fmt/format.h(轻量嵌入):
- vcpkg:
vcpkg install fmt,CMake 中find_package(fmt CONFIG) - 单头版:从 github.com/fmtlib/fmt 下载
include/fmt/format.h,直接#include "fmt/format.h"
最简示例:
#include "fmt/format.h" #includeint main() { std::string s = fmt::format("Hello, {}! You have {} messages.", "Alice", 42); std::cout << s << "\n"; // 输出:Hello, Alice! You have 42 messages. }
核心优势:类型安全 + 编译时检查
fmt 在编译期解析格式字符串,自动匹配参数类型,杜绝 printf 的 %d/%%s 错配或越界访问问题:
立即学习“C++免费学习笔记(深入)”;
// ✅ 安全:类型自动推导,无需手动指定格式符
fmt::print("Value: {}, Name: {}", 3.14, "pi");
// ❌ 编译失败:格式串与参数数量/类型不匹配
// fmt::print("{}", 1, 2); // error: too many args
// fmt::print("{:d}", 3.14); // error: 'd' not valid for double
进阶用法:对齐、精度、自定义类型
支持类似 Python 的丰富格式语法,且可扩展:
- 数字对齐:
{:>10}右对齐占10位,{:^8.2f}居中、宽度8、保留2位小数 - 十六进制/二进制:
{:x},{:b};带前缀:{:#x}→0xff - 自定义类型:为结构体特化
fmt::formatter,即可直接格式化
struct Point { int x, y; };
template <> struct fmt::formatter {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template
auto format(const Point& p, FormatContext& ctx) {
return fmt::format_to(ctx.out(), "({},{})", p.x, p.y);
}
};
// 使用:
fmt::print("Origin: {}\n", Point{0, 0}); // Origin: (0,0)
替代 iostream 和 printf 的典型场景
日常开发中可统一用 fmt::print 替代 std::cout 和 printf:
- 日志输出:
fmt::print(stderr, "[WARN] Failed to open {}: {}\n", path, err.msg()); - 性能敏感场景:
fmt::format_to支持写入预分配 buffer,避免临时 string 分配 - 国际化支持:配合
fmt::l10n模块做本地化格式(如货币、日期)
注意:若项目已用 C++20,可先试 std::format(接口几乎一致),但 fmt 功能更全、兼容性更好(支持 C++17+)。











