From 3d19be282a1c263a5ee0233425cc98a949fba770 Mon Sep 17 00:00:00 2001 From: timsong-cpp Date: Thu, 17 Mar 2022 23:53:48 -0500 Subject: [PATCH] Fix #2816: strip named argument wrappers for compile-time checking --- include/fmt/core.h | 11 ++++++++++- test/format-test.cc | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 1c81bbd2d980..2ea485599bba 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -2647,6 +2647,14 @@ FMT_CONSTEXPR FMT_INLINE void parse_format_string( } } +template ::value> struct strip_named_arg { + using type = T; +}; + +template struct strip_named_arg { + using type = remove_cvref_t; +}; + template FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx) -> decltype(ctx.begin()) { @@ -2654,7 +2662,8 @@ FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx) using context = buffer_context; using mapped_type = conditional_t< mapped_type_constant::value != type::custom_type, - decltype(arg_mapper().map(std::declval())), T>; + decltype(arg_mapper().map(std::declval())), + typename strip_named_arg::type>; auto f = conditional_t::value, formatter, fallback_formatter>(); diff --git a/test/format-test.cc b/test/format-test.cc index e7f65daf3275..09553af4e7aa 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1816,6 +1816,7 @@ TEST(format_test, compile_time_string) { "foo"_a = "foo")); EXPECT_EQ("", fmt::format(FMT_STRING(""))); EXPECT_EQ("", fmt::format(FMT_STRING(""), "arg"_a = 42)); + EXPECT_EQ("42", fmt::format(FMT_STRING("{answer}"), "answer"_a = Answer())); #endif (void)static_with_null; @@ -1885,6 +1886,8 @@ TEST(format_test, named_arg_udl) { fmt::format("{first}{second}{first}{third}", fmt::arg("first", "abra"), fmt::arg("second", "cad"), fmt::arg("third", 99)), udl_a); + + EXPECT_EQ("42", fmt::format("{answer}", "answer"_a = Answer())); } #endif // FMT_USE_USER_DEFINED_LITERALS