1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #include <format> #include <iostream> struct Frac { int a, b; };
template <> struct std::formatter<Frac, char> { template <typename ParseContext> constexpr auto parse(ParseContext& ctx) { return ctx.begin(); } template <typename FormatContext> auto format(const Frac& f, FormatContext& ctx) const { return std::format_to(ctx.out(), "{} / {}", f.a, f.b); } }; void print(const char* str, auto&&... args) { std::cout << std::vformat(str, std::make_format_args(std::forward<decltype(args)>(args)...)); } int main() { Frac f{1, 10}; print("{}", f); }
|