要让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 <format>
template<>
struct std::formatter<Point> {
// 解析格式说明符(例如 "{}" 或 "{:x}")
constexpr auto parse(std::format_parse_context& ctx) {
auto it = ctx.begin();
if (it != ctx.end() && *it != '}') {
format_spec = *it++; // 保存格式字符
}
return it;
}
// 格式化值到输出
template<typename FormatContext>
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:
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
还木有评论哦,快来抢沙发吧~