C++20的std::format怎么用_C++类型安全且可扩展的现代化字符串格式化库

c++kquote>C++20引入std::format,提供类型安全、可扩展的字符串格式化方式,替代不安全的printf;支持位置参数、格式说明符及自定义类型格式化,需特化std::formatter,编译器需支持C++20且注意标准库启用情况。

在C++20之前,字符串格式化主要依赖于C风格的printf或手动拼接,既不安全也不方便。C++20引入了std::format,基于Python的str.format()设计,提供类型安全、可扩展且高性能的格式化方式。

基本用法:替代printf的安全方式

std::format使用类似Python的占位符语法,编译时检查类型,避免缓冲区溢出和类型不匹配问题。

包含头文件:#include

// 示例:格式化字符串

std::string result = std::format("Hello, {}! You are {} years old.", "Alice", 25);
// 输出: Hello, Alice! You are 25 years old.

支持位置参数和命名参数:

  • std::format("{0}, {1}, {0}", "A", "B") → "A, B, A"
  • C++20目前不支持命名参数(如{name}),但提案已在路上

格式说明符:控制输出样式

{}中使用:后接格式说明符,控制对齐、精度、进制等。

  • 数值格式
    std::format("{:06d}", 42) → "000042"
    std::format("{:.2f}", 3.14159) → "3.14"
    std::format("{:x}", 255) → "ff"(十六进制)
  • 对齐与填充
    std::format("{:>8}", "hi") → " hi"(右对齐)
    std::format("{:_^10}", "hi") → "____hi____"(居中,下划线填充)
  • 时间格式(需chrono支持):
    std::format("{:%Y-%m-%d}", std::chrono::system_clock::now())

自定义类型支持格式化

要让自定义类型支持std::format,需特化std::formatter模板。

// 示例:为Point结构体添加格式化支持

struct Point {
    int x, y;
};

template
struct std::formatter {
    constexpr auto parse(auto& ctx) { return ctx.begin(); }

    auto format(const Point& p, auto& ctx) const {
        return std::format_to(ctx.out(), "({},{})", p.x, p.y);
    }
};

std::format("Position: {}", Point{1, 2}); // → Position: (1,2)

关键点:

  • parse:解析格式字符串(如支持:x:y可在此处理)
  • format:实际写入格式化内容,使用std::format_to写入输出迭代器

性能与注意事项

std::formatprintf稍慢但更安全,比流操作符更简洁高效。

  • 确保编译器支持C++20(GCC 13+, Clang 14+, MSVC 19.30+)
  • 某些标准库实现可能默认未启用(如libstdc++需定义__cpp_lib_format
  • 调试时可用std::vformat配合std::make_format_args实现动态格式化

基本上就这些。std::format统一了C++的格式化需求,类型安全又易于扩展,是现代C++字符串处理的推荐方式。