gpt4 book ai didi

c++ - 用户定义类型的 std::format ?

转载 作者:行者123 更新时间:2023-12-01 12:57:12 25 4
gpt4 key购买 nike

在 C++20 中 - 如何使用户定义的类型与 std::format 兼容?

例如,假设我有一个名为 Point 的类型。 :

struct Point {
int x;
int y;
};

与其 operator<<定义:
inline std::ostream&
operator<<(std::ostream& o, Point pt)
{ return o << "[" << pt.x << << ", " << pt.y << "]"; }

那么下面的程序会输出 Hello [3, 4]! ?
int main() {
Point pt{3,4};
std::cout << std::format("Hello {}!\n", pt);
}

如果是 - 为什么以及如何?

如果没有 - 我必须在 Point 的定义中添加什么让它发挥作用?

最佳答案

std::format不支持 operator<< ,您需要提供 formatter 而是针对您的类型 ( Point ) 进行特化。最简单的方法是重用现有的格式化程序之一,例如std::formatter<std::string> :

template <>
struct std::formatter<Point> : std::formatter<std::string> {
auto format(Point p, format_context& ctx) {
return formatter<string>::format(
std::format("[{}, {}]", p.x, p.y), ctx);
}
};

这将为您提供 std::string 支持的所有格式规范盒子外面。这是格式化 Point 的示例用 '~' 填充中心对齐到 10 个字符:
auto s = std::format("{:~^10}", Point{1, 2});
// s == "~~[1, 2]~~"

使用 iostreams 实现这一点非常重要。

关于c++ - 用户定义类型的 std::format ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59909102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com