From 1651b2d4338f285086fa35d6d5ca238f2782e49c Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Thu, 20 Aug 2020 16:41:09 +0300 Subject: [PATCH] Fix detail::write with fallback formatter (#1829) * add support for fallback_formatter in detail::write * add ToString test into OStreamTest to check fmt::to_string() with class that has output stream operator * add WithOstreamOperator test into CompileTest to check fmt::format() with FMT_COMPILE() and class that has output stream operator * use conditional_t inside detail::write instead of 2 overloads * Revert "add WithOstreamOperator test into CompileTest" * remove Context from template parameters in detail::write --- include/fmt/format.h | 9 +++++++-- test/ostream-test.cc | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 924e3700..5b7f6b52 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1847,8 +1847,13 @@ auto write(OutputIt out, const T& value) -> typename std::enable_if< mapped_type_constant>::value == type::custom_type, OutputIt>::type { - basic_format_context ctx(out, {}, {}); - return formatter().format(value, ctx); + using context_type = basic_format_context; + using formatter_type = + conditional_t::value, + typename context_type::template formatter_type, + fallback_formatter>; + context_type ctx(out, {}, {}); + return formatter_type().format(value, ctx); } // An argument visitor that formats the argument and writes it via the output diff --git a/test/ostream-test.cc b/test/ostream-test.cc index ce36b4cd..407b05a1 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -319,3 +319,7 @@ TEST(OStreamTest, CopyFmt) { TEST(OStreamTest, CompileTimeString) { EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42)); } + +TEST(OStreamTest, ToString) { + EXPECT_EQ("ABC", fmt::to_string(fmt_test::ABC())); +}