c++20的std::format如何自定义格式化_c++格式化用户定义类型

要让std::format支持自定义类型,需特化std::formatter并实现parse和format方法。以Point结构体为例,首先在全局或std命名空间中特化std::formatter,定义parse函数解析格式说明符(如"{:x}"中的'x'),保存格式选项;然后实现format函数,根据保存的格式符使用std::format_to将对象格式化为字符串。完成后即可用std::format格式化Point对象,如std::format("{}", p)输出"Point(10, 20)",支持自定义格式如":x"或":X"。注意需包含头文件,parse应正确处理格式上下文迭代器,format通过输出迭代器写入结果。此机制使用户类型能无缝集成到C++20格式化体系中。

在 C++20 中,std::format 提供了类型安全且高效的格式化方式。要让 std::format 支持用户自定义类型,你需要特化 std::formatter 模板,并实现相应的解析和格式化逻辑。

1. 特化 std::formatter 以支持自定义类型

假设你有一个简单的结构体 Point

struct Point {
    int x, y;
};

为了让 std::format 能够格式化 Point 类型,需要为它特化 std::formatter

#include 

template<>
struct std::formatter {
    // 解析格式说明符(例如 "{}" 或 "{:x}")
    constexpr auto parse(std::format_parse_context& ctx) {
        auto it = ctx.begin();
        if (it != ctx.end() && *it != '}') {
            format_spec = *it++;  // 保存格式字符
        }
        return it;
    }

    // 格式化值到输出
    template
    auto format(const Point& p, FormatContext& ctx) {
        if (format_spec == 'x') {
            return std::format_to(ctx.out(), "({}, {})", p.x, p.y);
        } else if (format_spec == 'X') {
            return std::format_to(ctx.out(), "[x={}, y={}]", p.x, p.y);
        } else {
            return std::format_to(ctx.out(), "Point({}, {})", p.x, p.y);
        }
    }

private:
    char format_spec = 0;  // 存储格式说明符
};

2. 使用自定义格式化类型

现在你可以像使用内置类型一样使用 std::format 来格式化 Point

#include 

int main() {
    Point p{10, 20};

    std::cout << std::format("{}\n", p);        // 输出: Point(10, 20)
    std::cout << std::format("{:x}\n", p);      // 输出: (10, 20)
    std::cout << std::format("{:X}\n", p);      // 输出: [x=10, y=20]
}

3. 注意事项与建议

  • 必须包含头文件
  • 特化的 std::formatter 必须在全局命名空间或 std 的同名空间中(通常推荐在全局)
  • parse() 函数负责处理格式字符串中的选项,应跳过无效字符并返回正确位置
  • format() 使用 std::format_to 将内容写入输出迭代器
  • 支持的格式说明符可以自由设计,如数字、字符、字符串等

基本上就这些。只要正确实现 parseformat 方法,任何用户定义类型都可以无缝集成进 std::format 体系。