Skip to content

Commit

Permalink
Fix fmtlib#2816: strip named argument wrappers for compile-time checking
Browse files Browse the repository at this point in the history
  • Loading branch information
timsong-cpp committed Mar 18, 2022
1 parent 6939634 commit 7207bd1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 10 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2647,14 +2647,23 @@ FMT_CONSTEXPR FMT_INLINE void parse_format_string(
}
}

template <typename T, bool = is_named_arg<T>::value> struct strip_named_arg {
using type = T;
};

template <typename T> struct strip_named_arg<T, true> {
using type = remove_cvref_t<decltype(T::value)>;
};

template <typename T, typename ParseContext>
FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx)
-> decltype(ctx.begin()) {
using char_type = typename ParseContext::char_type;
using context = buffer_context<char_type>;
using mapped_type = conditional_t<
mapped_type_constant<T, context>::value != type::custom_type,
decltype(arg_mapper<context>().map(std::declval<const T&>())), T>;
decltype(arg_mapper<context>().map(std::declval<const T&>())),
typename strip_named_arg<T>::type>;
auto f = conditional_t<has_formatter<mapped_type, context>::value,
formatter<mapped_type, char_type>,
fallback_formatter<T, char_type>>();
Expand Down
3 changes: 3 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 7207bd1

Please sign in to comment.