在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::formatter<Ti, CharT>
。 - unegare