mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Fix format_as for non-const begin/end views (#3955)
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
This commit is contained in:
@ -4000,7 +4000,9 @@ struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>>
|
|||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
|
auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||||
using base = formatter<detail::format_as_t<T>, Char>;
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "fmt/ranges.h"
|
#include "fmt/ranges.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
@ -654,18 +655,18 @@ TEST(ranges_test, lvalue_qualified_begin_end) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(__cpp_lib_ranges) || __cpp_lib_ranges <= 202106L
|
#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
|
#elif FMT_CLANG_VERSION
|
||||||
# if FMT_CLANG_VERSION > 1500
|
# if FMT_CLANG_VERSION > 1500
|
||||||
# define ENABLE_INPUT_RANGE_JOIN_TEST 1
|
# define ENABLE_STD_VIEWS_TESTS 1
|
||||||
# else
|
# else
|
||||||
# define ENABLE_INPUT_RANGE_JOIN_TEST 0
|
# define ENABLE_STD_VIEWS_TESTS 0
|
||||||
# endif
|
# endif
|
||||||
#else
|
#else
|
||||||
# define ENABLE_INPUT_RANGE_JOIN_TEST 1
|
# define ENABLE_STD_VIEWS_TESTS 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ENABLE_INPUT_RANGE_JOIN_TEST
|
#if ENABLE_STD_VIEWS_TESTS
|
||||||
TEST(ranges_test, input_range_join) {
|
TEST(ranges_test, input_range_join) {
|
||||||
auto iss = std::istringstream("1 2 3 4 5");
|
auto iss = std::istringstream("1 2 3 4 5");
|
||||||
auto view = std::views::istream<std::string>(iss);
|
auto view = std::views::istream<std::string>(iss);
|
||||||
@ -679,6 +680,42 @@ TEST(ranges_test, input_range_join_overload) {
|
|||||||
"1.2.3.4.5",
|
"1.2.3.4.5",
|
||||||
fmt::format("{}", fmt::join(std::views::istream<std::string>(iss), ".")));
|
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
|
#endif
|
||||||
|
|
||||||
TEST(ranges_test, std_istream_iterator_join) {
|
TEST(ranges_test, std_istream_iterator_join) {
|
||||||
|
Reference in New Issue
Block a user