Skip to content

Commit

Permalink
Fix format_as for non-const begin/end views
Browse files Browse the repository at this point in the history
base::format does not accept rvalues, using the result of format_as
directly means it is passed one. Doesn't matter for ranges with a const
begin and end, but filter_view caches, so it only has mutable begin/end.
  • Loading branch information
Arghnews committed May 11, 2024
1 parent 1dc71f2 commit cb057a4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
4 changes: 3 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4000,7 +4000,9 @@ struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>>
template <typename FormatContext>
auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
using base = formatter<detail::format_as_t<T>, Char>;
return base::format(format_as(value), ctx);
// format functions expect lvalue refs, not rvalues
auto val = format_as(value);
return base::format(val, ctx);
}
};

Expand Down
47 changes: 42 additions & 5 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "fmt/ranges.h"

#include <array>
#include <list>
#include <map>
#include <numeric>
Expand Down Expand Up @@ -654,18 +655,18 @@ TEST(ranges_test, lvalue_qualified_begin_end) {
}

#if !defined(__cpp_lib_ranges) || __cpp_lib_ranges <= 202106L
# define ENABLE_INPUT_RANGE_JOIN_TEST 0
# define ENABLE_STD_VIEWS_TESTS 0
#elif FMT_CLANG_VERSION
# if FMT_CLANG_VERSION > 1500
# define ENABLE_INPUT_RANGE_JOIN_TEST 1
# define ENABLE_STD_VIEWS_TESTS 1
# else
# define ENABLE_INPUT_RANGE_JOIN_TEST 0
# define ENABLE_STD_VIEWS_TESTS 0
# endif
#else
# define ENABLE_INPUT_RANGE_JOIN_TEST 1
# define ENABLE_STD_VIEWS_TESTS 1
#endif

#if ENABLE_INPUT_RANGE_JOIN_TEST
#if ENABLE_STD_VIEWS_TESTS
TEST(ranges_test, input_range_join) {
auto iss = std::istringstream("1 2 3 4 5");
auto view = std::views::istream<std::string>(iss);
Expand All @@ -679,6 +680,42 @@ TEST(ranges_test, input_range_join_overload) {
"1.2.3.4.5",
fmt::format("{}", fmt::join(std::views::istream<std::string>(iss), ".")));
}

namespace views_filter_view_test {
struct codec_mask {
static constexpr auto kCodecs = std::array{0, 1, 2, 3};
int except{};
};

auto format_as(codec_mask mask) {
// Careful not to capture param by reference here, it will dangle
return codec_mask::kCodecs |
std::views::filter([mask](auto c) { return c != mask.except; });
}
} // namespace views_filter_view_test

TEST(ranges_test, format_as_with_ranges_mutable_begin_end) {
using namespace views_filter_view_test;
{
auto make_filter_view = []() {
return codec_mask::kCodecs |
std::views::filter([](auto c) { return c != 2; });
};
auto r = make_filter_view();
EXPECT_EQ("[0, 1, 3]", fmt::format("{}", r));
EXPECT_EQ("[0, 1, 3]", fmt::format("{}", make_filter_view()));
}

{
const codec_mask const_mask{2};
codec_mask mask{2};

EXPECT_EQ("[0, 1, 3]", fmt::format("{}", const_mask));
EXPECT_EQ("[0, 1, 3]", fmt::format("{}", mask));
EXPECT_EQ("[0, 1, 3]", fmt::format("{}", codec_mask{2}));
}
}

#endif

TEST(ranges_test, std_istream_iterator_join) {
Expand Down

0 comments on commit cb057a4

Please sign in to comment.