From 61097cc3d29764413e14bb9347fe715e5c85fc3e 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 | 13 ++++++++++++- test/format-test.cc | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 1c81bbd2d9804..4cb09bed6622d 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -2647,6 +2647,16 @@ 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 +2664,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 e7f65daf32751..fc57dde325085 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