【发布时间】:2020-01-25 12:14:03
【问题描述】:
在 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 的定义中添加什么才能使其正常工作?
【问题讨论】:
-
@rafix07:您确定该功能已被移植到 C++20 中吗?
-
你应该重载
std::formatter<Ti, CharT> -
别忘了投票 =)。