mirror of
https://github.com/fmtlib/fmt.git
synced 2025-11-25 03:39:56 +01:00
Implement nested formatter
This commit is contained in:
@@ -1779,22 +1779,25 @@ TEST(format_test, group_digits_view) {
|
||||
EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(1000)), " 1,000");
|
||||
}
|
||||
|
||||
#ifdef __cpp_generic_lambdas
|
||||
struct point {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
template <>
|
||||
struct formatter<point> : nested_formatter<double> {
|
||||
template <> struct formatter<point> : nested_formatter<double> {
|
||||
auto format(point p, format_context& ctx) const -> decltype(ctx.out()) {
|
||||
return format_to(ctx.out(), "({}, {})", nested(p.x), nested(p.y));
|
||||
return write_padded(ctx, [this, p](auto out) -> decltype(out) {
|
||||
return format_to(out, "({}, {})", nested(p.x), nested(p.y));
|
||||
});
|
||||
}
|
||||
};
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
TEST(format_test, nested_formatter) {
|
||||
EXPECT_EQ(fmt::format("{:.2f}", point{1, 2}), "(1.00, 2.00)");
|
||||
EXPECT_EQ(fmt::format("{:>16.2f}", point{1, 2}), " (1.00, 2.00)");
|
||||
}
|
||||
#endif // __cpp_generic_lambdas
|
||||
|
||||
enum test_enum { foo, bar };
|
||||
auto format_as(test_enum e) -> int { return e; }
|
||||
|
||||
Reference in New Issue
Block a user