diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index d4501529..43f89d00 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -482,7 +482,8 @@ struct range_formatter< for (; it != end; ++it) { if (i > 0) out = detail::copy_str(separator_, out); ctx.advance_to(out); - out = underlying_.format(mapper.map(*it), ctx); + auto&& item = *it; + out = underlying_.format(mapper.map(item), ctx); ++i; } out = detail::copy_str(closing_bracket_, out); diff --git a/test/ranges-test.cc b/test/ranges-test.cc index c67eed9f..c161fbc4 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -7,13 +7,19 @@ #include "fmt/ranges.h" +#include #include +#include #include #include #include #include #include +#if FMT_HAS_INCLUDE() +# include +#endif + #include "gtest/gtest.h" #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 601 @@ -242,7 +248,7 @@ template class non_const_only_range { explicit non_const_only_range(Args&&... args) : vec(std::forward(args)...) {} - auto begin() -> const_iterator{ return vec.begin(); } + auto begin() -> const_iterator { return vec.begin(); } auto end() -> const_iterator { return vec.end(); } }; @@ -360,7 +366,7 @@ struct cpp20_only_range { iterator() = default; iterator(int i) : val(i) {} auto operator*() const -> int { return val; } - auto operator++() -> iterator&{ + auto operator++() -> iterator& { ++val; return *this; } @@ -378,6 +384,17 @@ struct cpp20_only_range { }; static_assert(std::input_iterator); + +# ifdef __cpp_lib_ranges_iota +TEST(ranges_test, nested_ranges) { + auto l = std::list{1, 2, 3}; + auto r = std::views::iota(0, 3) | std::views::transform([&l](auto i) { + return std::views::take(std::ranges::subrange(l), i); + }) | + std::views::transform(std::views::reverse); + EXPECT_EQ(fmt::format("{}", r), "[[], [1], [2, 1]]"); +} +# endif # endif TEST(ranges_test, join_sentinel) {