[libcxx-commits] [libcxx] 4851fbc - fix errors on passing input iterator to `std::views::take`

Hui Xie via libcxx-commits libcxx-commits at lists.llvm.org
Sun Sep 25 07:43:35 PDT 2022


Author: Hui Xie
Date: 2022-09-25T15:41:13+01:00
New Revision: 4851fbc3cf2387fd5f3f09a935fab9397282215b

URL: https://github.com/llvm/llvm-project/commit/4851fbc3cf2387fd5f3f09a935fab9397282215b
DIFF: https://github.com/llvm/llvm-project/commit/4851fbc3cf2387fd5f3f09a935fab9397282215b.diff

LOG: fix errors on passing input iterator to `std::views::take`

In the implementation of `std::views::take`, it uses `subrange<Iter>` as part of the return type. But in case of input iterator, `subrange<Iter>` can be ill-formed

Differential Revision: https://reviews.llvm.org/D133220

Added: 
    

Modified: 
    libcxx/include/__ranges/take_view.h
    libcxx/test/std/ranges/range.adaptors/range.take/adaptor.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__ranges/take_view.h b/libcxx/include/__ranges/take_view.h
index 4bc7bf5e0115a..3fb9499c0118a 100644
--- a/libcxx/include/__ranges/take_view.h
+++ b/libcxx/include/__ranges/take_view.h
@@ -226,6 +226,7 @@ struct __passthrough_type<basic_string_view<_CharT, _Traits>> {
 };
 
 template <class _Iter, class _Sent, subrange_kind _Kind>
+  requires requires{typename subrange<_Iter>;}
 struct __passthrough_type<subrange<_Iter, _Sent, _Kind>> {
   using type = subrange<_Iter>;
 };

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 40b6a3d8ca0ea..9f4a5632c82b6 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
@@ -194,6 +194,19 @@ constexpr bool test() {
     [[maybe_unused]] auto partial = std::views::take(X{});
   }
 
+  // Test when `subrange<Iter>` is not well formed
+  {
+    int input[] = {1, 2, 3};
+    using Iter  = cpp20_input_iterator<int*>;
+    using Sent  = sentinel_wrapper<Iter>;
+    std::ranges::subrange r{Iter{input}, Sent{Iter{input + 3}}};
+    auto tv = std::views::take(std::move(r), 1);
+    auto it                  = tv.begin();
+    assert(*it == 1);
+    ++it;
+    assert(it == tv.end());
+  }
+
   return true;
 }
 


        


More information about the libcxx-commits mailing list