Skip to content
forked from fmtlib/fmt

Commit

Permalink
Make compile-time checks in format_to handle references
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 1, 2020
1 parent 58e6c84 commit 1e84931
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ make_args_checked(const S& format_str,
all_true<(!std::is_base_of<view, remove_reference_t<Args>>::value ||
!std::is_reference<Args>::value)...>::value,
"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...};
}

Expand Down
7 changes: 3 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2641,10 +2641,9 @@ FMT_CONSTEXPR bool do_check_format_string(basic_string_view<Char> s,
template <typename... Args, typename S,
enable_if_t<(is_compile_string<S>::value), int>>
void check_format_string(S format_str) {
FMT_CONSTEXPR_DECL bool invalid_format =
internal::do_check_format_string<typename S::char_type,
internal::error_handler, Args...>(
to_string_view(format_str));
FMT_CONSTEXPR_DECL bool invalid_format = internal::do_check_format_string<
typename S::char_type, internal::error_handler,
remove_const_t<remove_reference_t<Args>>...>(to_string_view(format_str));
(void)invalid_format;
}

Expand Down
2 changes: 2 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,8 @@ TEST(FormatTest, CustomFormatCompileTimeString) {
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), Answer()));
Answer answer;
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), answer));
char buf[10] = {};
fmt::format_to(buf, FMT_STRING("{}"), answer);
const Answer const_answer = Answer();
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), const_answer));
}
Expand Down

0 comments on commit 1e84931

Please sign in to comment.