From c04fb91b03cb6480c4a39b214efb6b05e452b20c Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 4 Jul 2018 07:40:56 -0700 Subject: [PATCH] Fix handling of user-defined types in format_to (#793) --- include/fmt/format.h | 5 ++--- test/format-test.cc | 10 +++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index f1918a401bd5..d5d65817128c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3216,9 +3216,8 @@ struct formatter< specs_.precision_, specs_.precision_ref, ctx); typedef output_range range_type; - visit(arg_formatter(ctx, specs_), - internal::make_arg(val)); - return ctx.out(); + return visit(arg_formatter(ctx, specs_), + internal::make_arg(val)); } private: diff --git a/test/format-test.cc b/test/format-test.cc index e2d62c304295..39855ae8dc80 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1122,7 +1122,8 @@ class Answer {}; FMT_BEGIN_NAMESPACE template <> struct formatter : formatter { - auto format(Answer, fmt::format_context &ctx) -> decltype(ctx.out()) { + template + auto format(Answer, FormatContext &ctx) -> decltype(ctx.out()) { return formatter::format(42, ctx); } }; @@ -1133,6 +1134,13 @@ TEST(FormatterTest, CustomFormat) { EXPECT_EQ("0042", format("{:04}", Answer())); } +TEST(FormatterTest, CustomFormatTo) { + char buf[10] = {}; + auto end = fmt::format_to(buf, "{}", Answer()); + EXPECT_EQ(end, buf + 2); + EXPECT_STREQ(buf, "42"); +} + TEST(FormatterTest, WideFormatString) { EXPECT_EQ(L"42", format(L"{}", 42)); EXPECT_EQ(L"4.2", format(L"{}", 4.2));