[libcxx-commits] [libcxx] [libc++] Fix the handling of `views::take` for `iota_view` (PR #75683)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 15 19:16:49 PST 2023


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/75683

Fixes #75611.

>From db8bbfc8b08673bb3c9637ea75071286c8f4ca48 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Sat, 16 Dec 2023 11:15:48 +0800
Subject: [PATCH] Fix the handling of `views::take` for `iota_view`

---
 libcxx/include/__ranges/take_view.h                         | 6 +++---
 .../std/ranges/range.adaptors/range.take/adaptor.pass.cpp   | 5 ++---
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/__ranges/take_view.h b/libcxx/include/__ranges/take_view.h
index 518375d684abdd..d2764923fad366 100644
--- a/libcxx/include/__ranges/take_view.h
+++ b/libcxx/include/__ranges/take_view.h
@@ -290,16 +290,16 @@ struct __fn {
   constexpr auto operator()(_Range&& __rng, _Np&& __n) const
     noexcept(noexcept(ranges::iota_view(
                               *ranges::begin(__rng),
-                              *ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n))
+                              *(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
                               )))
     -> decltype(      ranges::iota_view(
                               // Note: deliberately not forwarding `__rng` to guard against double moves.
                               *ranges::begin(__rng),
-                              *ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n))
+                              *(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
                               ))
     { return          ranges::iota_view(
                               *ranges::begin(__rng),
-                              *ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n))
+                              *(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
                               ); }
 // clang-format off
 #if _LIBCPP_STD_VER >= 23
diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/adaptor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/adaptor.pass.cpp
index 8ffac8d8fab153..7c108bd5d0a9aa 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/adaptor.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/adaptor.pass.cpp
@@ -172,9 +172,8 @@ constexpr bool test() {
   // `views::take(iota_view, n)` returns an `iota_view`.
   {
     auto iota = std::views::iota(1, 8);
-    // The second template argument of the resulting `iota_view` is different because it has to be able to hold
-    // the `range_difference_t` of the input `iota_view`.
-    using Result = std::ranges::iota_view<int, std::ranges::range_difference_t<decltype(iota)>>;
+    // The second template argument of the resulting `iota_view` is same as the first.
+    using Result = std::ranges::iota_view<int, int>;
     std::same_as<Result> decltype(auto) result = iota | std::views::take(3);
     assert(result.size() == 3);
   }



More information about the libcxx-commits mailing list