From: Hui Xie Date: Fri, 2 Sep 2022 18:24:22 +0000 (+0100) Subject: fix errors on passing input iterator to `std::views::take` X-Git-Tag: upstream/17.0.6~32565 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4851fbc3cf2387fd5f3f09a935fab9397282215b;p=platform%2Fupstream%2Fllvm.git fix errors on passing input iterator to `std::views::take` In the implementation of `std::views::take`, it uses `subrange` as part of the return type. But in case of input iterator, `subrange` can be ill-formed Differential Revision: https://reviews.llvm.org/D133220 --- diff --git a/libcxx/include/__ranges/take_view.h b/libcxx/include/__ranges/take_view.h index 4bc7bf5..3fb9499 100644 --- a/libcxx/include/__ranges/take_view.h +++ b/libcxx/include/__ranges/take_view.h @@ -226,6 +226,7 @@ struct __passthrough_type> { }; template + requires requires{typename subrange<_Iter>;} struct __passthrough_type> { 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 40b6a3d..9f4a563 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` is not well formed + { + int input[] = {1, 2, 3}; + using Iter = cpp20_input_iterator; + using Sent = sentinel_wrapper; + 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; }