diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 2a07d2f670f7..7115df3ec3ec 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -654,6 +654,16 @@ format_to_n_result format_to_n(OutputIt out, size_t n, return {it.base(), it.count()}; } +template ::value)> +format_to_n_result format_to_n(OutputIt out, size_t n, const S&, + const Args&... args) { + constexpr auto compiled = detail::compile(S()); + auto it = format_to(detail::truncating_iterator(out, n), compiled, + args...); + return {it.base(), it.count()}; +} + template size_t formatted_size(const CompiledFormat& cf, const Args&... args) { return format_to(detail::counting_iterator(), cf, args...).count(); diff --git a/test/compile-test.cc b/test/compile-test.cc index a47cea548fc7..a8c7f789da90 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -160,6 +160,14 @@ TEST(CompileTest, FormatTo) { EXPECT_STREQ("42", buf); } +TEST(CompileTest, FormatToNWithCompileMacro) { + constexpr auto buffer_size = 8; + char buffer[buffer_size]; + auto res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{}"), 42); + *res.out = '\0'; + EXPECT_STREQ("42", buffer); +} + TEST(CompileTest, TextAndArg) { EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42)); }