diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 3a16890f..1fbc9953 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -206,8 +206,7 @@ template class basic_printf_context; \endrst */ template -class printf_arg_formatter - : public detail::arg_formatter_base { +class printf_arg_formatter : public detail::arg_formatter_base { public: using iterator = OutputIt; @@ -592,7 +591,7 @@ OutputIt basic_printf_context::format() { start = it; // Format argument. - visit_format_arg(ArgFormatter(out, specs, *this), arg); + out = visit_format_arg(ArgFormatter(out, specs, *this), arg); } return std::copy(start, it, out); } diff --git a/test/printf-test.cc b/test/printf-test.cc index 1c1173c4..ccd72dcd 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -606,3 +606,23 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) { {fmt::make_wprintf_args(42, L"something")})); #endif } + +TEST(PrintfTest, PrintfDetermineOutputSize) { + using backit = std::back_insert_iterator>; + using truncated_printf_context = + fmt::basic_printf_context, char>; + + auto v = std::vector{}; + auto it = std::back_inserter(v); + + const auto format_string = "%s"; + const auto format_arg = "Hello"; + const auto expected_size = fmt::sprintf(format_string, format_arg).size(); + + EXPECT_EQ((truncated_printf_context( + fmt::detail::truncating_iterator(it, 0), format_string, + fmt::make_format_args(format_arg)) + .format() + .count()), + expected_size); +}