mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-31 03:07:36 +02:00
Fix #2816: strip named argument wrappers for compile-time checking
This commit is contained in:
committed by
Victor Zverovich
parent
c076a54a4d
commit
3d19be282a
@ -2647,6 +2647,14 @@ 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>
|
template <typename T, typename ParseContext>
|
||||||
FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx)
|
FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx)
|
||||||
-> decltype(ctx.begin()) {
|
-> decltype(ctx.begin()) {
|
||||||
@ -2654,7 +2662,8 @@ FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx)
|
|||||||
using context = buffer_context<char_type>;
|
using context = buffer_context<char_type>;
|
||||||
using mapped_type = conditional_t<
|
using mapped_type = conditional_t<
|
||||||
mapped_type_constant<T, context>::value != type::custom_type,
|
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,
|
auto f = conditional_t<has_formatter<mapped_type, context>::value,
|
||||||
formatter<mapped_type, char_type>,
|
formatter<mapped_type, char_type>,
|
||||||
fallback_formatter<T, char_type>>();
|
fallback_formatter<T, char_type>>();
|
||||||
|
@ -1816,6 +1816,7 @@ TEST(format_test, compile_time_string) {
|
|||||||
"foo"_a = "foo"));
|
"foo"_a = "foo"));
|
||||||
EXPECT_EQ("", fmt::format(FMT_STRING("")));
|
EXPECT_EQ("", fmt::format(FMT_STRING("")));
|
||||||
EXPECT_EQ("", fmt::format(FMT_STRING(""), "arg"_a = 42));
|
EXPECT_EQ("", fmt::format(FMT_STRING(""), "arg"_a = 42));
|
||||||
|
EXPECT_EQ("42", fmt::format(FMT_STRING("{answer}"), "answer"_a = Answer()));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
(void)static_with_null;
|
(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::format("{first}{second}{first}{third}", fmt::arg("first", "abra"),
|
||||||
fmt::arg("second", "cad"), fmt::arg("third", 99)),
|
fmt::arg("second", "cad"), fmt::arg("third", 99)),
|
||||||
udl_a);
|
udl_a);
|
||||||
|
|
||||||
|
EXPECT_EQ("42", fmt::format("{answer}", "answer"_a = Answer()));
|
||||||
}
|
}
|
||||||
#endif // FMT_USE_USER_DEFINED_LITERALS
|
#endif // FMT_USE_USER_DEFINED_LITERALS
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user