From ae42fbd5aa295937848e92cbf2406709f2d7ecd1 Mon Sep 17 00:00:00 2001 From: Justin Riddell Date: Mon, 13 May 2024 03:12:03 +0100 Subject: [PATCH] Fix format_as for non-const begin/end views 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. Use auto&& to avoid copy if format_as returns const ref --- include/fmt/format.h | 4 +++- test/ranges-test.cc | 47 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index da3b7f13a5b7..b123bd22b370 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -4000,7 +4000,9 @@ struct formatter::value>> template auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) { using base = formatter, 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); } }; diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 6df1ee763efc..8ab6c60eba0d 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -7,6 +7,7 @@ #include "fmt/ranges.h" +#include #include #include #include @@ -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(iss); @@ -679,6 +680,42 @@ TEST(ranges_test, input_range_join_overload) { "1.2.3.4.5", fmt::format("{}", fmt::join(std::views::istream(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) {