In C++20 - how do you make a user-defined type compatible with std::format
?
For example, let's say I have a type called Point
:
struct Point {
int x;
int y;
};
with its operator<<
defined:
inline std::ostream&
operator<<(std::ostream& o, Point pt)
{ return o << "[" << pt.x << << ", " << pt.y << "]"; }
then will the following program output Hello [3, 4]!
?
int main() {
Point pt{3,4};
std::cout << std::format("Hello {}!\n", pt);
}
If yes - why and how?
If no - what do I have to add to the definition of Point
to to make it work?
std::formatter<Ti, CharT>
– Carbon