Make compile-time checks in format_to handle references

This commit is contained in:
Victor Zverovich 2020-03-01 07:57:34 -08:00
parent 58e6c84f5a
commit 1e8493196e
3 changed files with 6 additions and 5 deletions

View File

@ -1460,7 +1460,7 @@ make_args_checked(const S& format_str,
all_true<(!std::is_base_of<view, remove_reference_t<Args>>::value || all_true<(!std::is_base_of<view, remove_reference_t<Args>>::value ||
!std::is_reference<Args>::value)...>::value, !std::is_reference<Args>::value)...>::value,
"passing views as lvalues is disallowed"); "passing views as lvalues is disallowed");
check_format_string<remove_const_t<remove_reference_t<Args>>...>(format_str); check_format_string<Args...>(format_str);
return {args...}; return {args...};
} }

View File

@ -2641,10 +2641,9 @@ FMT_CONSTEXPR bool do_check_format_string(basic_string_view<Char> s,
template <typename... Args, typename S, template <typename... Args, typename S,
enable_if_t<(is_compile_string<S>::value), int>> enable_if_t<(is_compile_string<S>::value), int>>
void check_format_string(S format_str) { void check_format_string(S format_str) {
FMT_CONSTEXPR_DECL bool invalid_format = FMT_CONSTEXPR_DECL bool invalid_format = internal::do_check_format_string<
internal::do_check_format_string<typename S::char_type, typename S::char_type, internal::error_handler,
internal::error_handler, Args...>( remove_const_t<remove_reference_t<Args>>...>(to_string_view(format_str));
to_string_view(format_str));
(void)invalid_format; (void)invalid_format;
} }

View File

@ -1859,6 +1859,8 @@ TEST(FormatTest, CustomFormatCompileTimeString) {
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), Answer())); EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), Answer()));
Answer answer; Answer answer;
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), answer)); EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), answer));
char buf[10] = {};
fmt::format_to(buf, FMT_STRING("{}"), answer);
const Answer const_answer = Answer(); const Answer const_answer = Answer();
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), const_answer)); EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), const_answer));
} }