Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fmtlib#2816: strip named argument wrappers for compile-time checking
Browse files Browse the repository at this point in the history
timsong-cpp committed Mar 18, 2022
1 parent 6939634 commit 61097cc
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
@@ -2647,14 +2647,25 @@ FMT_CONSTEXPR FMT_INLINE void parse_format_string(
}
}

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

template<class 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>>();
3 changes: 3 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 61097cc

Please sign in to comment.