diff --git a/libcxx/docs/Status/RangesPaper.csv b/libcxx/docs/Status/RangesPaper.csv --- a/libcxx/docs/Status/RangesPaper.csv +++ b/libcxx/docs/Status/RangesPaper.csv @@ -139,7 +139,7 @@ `[range.filter] `_,filter_view,[range.all],Louis Dionne,Not started `[range.transform] `_,`transform_view `_,[range.all],Zoe Carver,✅ `[range.iota] `_,iota_view,[range.all],Louis Dionne,Not started -`[range.take] `_,take_view,[range.all],Zoe Carver,In Progress +`[range.take] `_,take_view,[range.all],Zoe Carver,✅ `[range.join] `_,join_view,[range.all],Christopher Di Bella,Not started `[range.empty] `_,`empty_view `_,[view.interface],Zoe Carver,✅ `[range.single] `_,single_view,[view.interface],Unassigned,Not started diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -195,6 +195,7 @@ __ranges/enable_borrowed_range.h __ranges/enable_view.h __ranges/ref_view.h + __ranges/take_view.h __ranges/size.h __ranges/subrange.h __ranges/transform_view.h diff --git a/libcxx/include/__iterator/counted_iterator.h b/libcxx/include/__iterator/counted_iterator.h --- a/libcxx/include/__iterator/counted_iterator.h +++ b/libcxx/include/__iterator/counted_iterator.h @@ -59,7 +59,7 @@ template struct __counted_iterator_value_type<_Iter> { - using value_type = typename _Iter::value_type; + using value_type = iter_value_t<_Iter>; }; template diff --git a/libcxx/include/__ranges/common_view.h b/libcxx/include/__ranges/common_view.h --- a/libcxx/include/__ranges/common_view.h +++ b/libcxx/include/__ranges/common_view.h @@ -11,7 +11,12 @@ #include <__config> #include <__iterator/common_iterator.h> +#include <__iterator/iterator_traits.h> +#include <__ranges/access.h> +#include <__ranges/all.h> #include <__ranges/concepts.h> +#include <__ranges/enable_borrowed_range.h> +#include <__ranges/size.h> #include <__ranges/view_interface.h> #include #include @@ -79,10 +84,9 @@ } }; -// TODO: use all_t here when it's implemented. template common_view(_Range&&) - -> common_view()))>; + -> common_view>; template inline constexpr bool enable_borrowed_range> = enable_borrowed_range<_View>; diff --git a/libcxx/include/__ranges/take_view.h b/libcxx/include/__ranges/take_view.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__ranges/take_view.h @@ -0,0 +1,171 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#ifndef _LIBCPP___RANGES_TAKE_VIEW_H +#define _LIBCPP___RANGES_TAKE_VIEW_H + +#include <__config> +#include <__iterator/counted_iterator.h> +#include <__iterator/iterator_traits.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/view_interface.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_HAS_NO_RANGES) + +namespace ranges { + template + class take_view : public view_interface> { + _View __base_ = _View(); + range_difference_t<_View> __count_ = 0; + + template class __sentinel; + + public: + take_view() = default; + + _LIBCPP_HIDE_FROM_ABI + constexpr take_view(_View __base, range_difference_t<_View> __count) + : __base_(_VSTD::move(__base)), __count_(__count) {} + + _LIBCPP_HIDE_FROM_ABI + constexpr _View base() const& requires copy_constructible<_View> { return __base_; } + + _LIBCPP_HIDE_FROM_ABI + constexpr _View base() && { return _VSTD::move(__base_); } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto begin() requires (!__simple_view<_View>) { + if constexpr (sized_range<_View>) { + if constexpr (random_access_range<_View>) { + return ranges::begin(__base_); + } else { + auto __size = size(); + return counted_iterator{ranges::begin(__base_), static_cast>(__size)}; + } + } else { + return counted_iterator{ranges::begin(__base_), __count_}; + } + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto begin() const requires range { + if constexpr (sized_range) { + if constexpr (random_access_range) { + return ranges::begin(__base_); + } else { + auto __size = size(); + return counted_iterator{ranges::begin(__base_), static_cast>(__size)}; + } + } else { + return counted_iterator{ranges::begin(__base_), __count_}; + } + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto end() requires (!__simple_view<_View>) { + if constexpr (sized_range<_View>) { + if constexpr (random_access_range<_View>) { + return ranges::begin(__base_) + size(); + } else { + return default_sentinel; + } + } else { + return __sentinel{ranges::end(__base_)}; + } + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto end() const requires range { + if constexpr (sized_range) { + if constexpr (random_access_range) { + return ranges::begin(__base_) + size(); + } else { + return default_sentinel; + } + } else { + return __sentinel{ranges::end(__base_)}; + } + } + + + _LIBCPP_HIDE_FROM_ABI + constexpr auto size() requires sized_range<_View> { + auto __n = ranges::size(__base_); + return _VSTD::min(__n, static_cast(__count_)); + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto size() const requires sized_range { + auto __n = ranges::size(__base_); + return _VSTD::min(__n, static_cast(__count_)); + } + }; + + template + template + class take_view<_View>::__sentinel { + using _Base = __maybe_const<_Const, _View>; + template + using _Iter = counted_iterator>>; + sentinel_t<_Base> __end_ = sentinel_t<_Base>(); + + template + friend class take_view<_View>::__sentinel; + +public: + __sentinel() = default; + + _LIBCPP_HIDE_FROM_ABI + constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(__end) {} // TODO: why not move this one too? + + _LIBCPP_HIDE_FROM_ABI + constexpr __sentinel(__sentinel __s) + requires _Const && convertible_to, sentinel_t<_Base>> + : __end_(_VSTD::move(__s.__end_)) {} + + _LIBCPP_HIDE_FROM_ABI + constexpr sentinel_t<_Base> base() const { return __end_; } + + _LIBCPP_HIDE_FROM_ABI + friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) { + return __lhs.count() == 0 || __lhs.base() == __rhs.base(); + } + + template + requires sentinel_for, iterator_t<__maybe_const<_OtherConst, _View>>> + _LIBCPP_HIDE_FROM_ABI + friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) { + return __lhs.count() == 0 || __lhs.base() == __rhs.base(); + } + }; + + template + take_view(_Range&&, range_difference_t<_Range>) -> take_view>; + + template + inline constexpr bool enable_borrowed_range> = enable_borrowed_range<_Tp>; +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___RANGES_TAKE_VIEW_H diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -617,6 +617,7 @@ module ref_view { private header "__ranges/ref_view.h" } module size { private header "__ranges/size.h" } module subrange { private header "__ranges/subrange.h" } + module take_view { private header "__ranges/take_view.h" } module transform_view { private header "__ranges/transform_view.h" } module view_interface { private header "__ranges/view_interface.h" } } diff --git a/libcxx/include/ranges b/libcxx/include/ranges --- a/libcxx/include/ranges +++ b/libcxx/include/ranges @@ -144,6 +144,12 @@ template inline constexpr bool enable_borrowed_range> = enable_borrowed_range; + + // [range.take], take view + template class take_view; + + template + inline constexpr bool enable_borrowed_range> = enable_borrowed_range; } */ @@ -160,6 +166,7 @@ #include <__ranges/enable_borrowed_range.h> #include <__ranges/enable_view.h> #include <__ranges/ref_view.h> +#include <__ranges/take_view.h> #include <__ranges/size.h> #include <__ranges/subrange.h> #include <__ranges/transform_view.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/iterator/common_iterator.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/iterator/common_iterator.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/iterator/common_iterator.module.verify.cpp @@ -0,0 +1,16 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__iterator/common_iterator.h'}} +#include <__iterator/common_iterator.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp @@ -0,0 +1,16 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__iterator/counted_iterator.h'}} +#include <__iterator/counted_iterator.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/ranges/common_view.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/ranges/common_view.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/ranges/common_view.module.verify.cpp @@ -0,0 +1,16 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__ranges/common_view.h'}} +#include <__ranges/common_view.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/ranges/take_view.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/ranges/take_view.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/ranges/take_view.module.verify.cpp @@ -0,0 +1,16 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__ranges/take_view.h'}} +#include <__ranges/take_view.h> diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/base.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/base.pass.cpp @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// constexpr V base() const& requires copy_constructible; +// constexpr V base() &&; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct ContiguousView : std::ranges::view_base { + int *ptr_; + constexpr ContiguousView(int* ptr) : ptr_(ptr) {} + constexpr ContiguousView(ContiguousView&&) = default; + constexpr ContiguousView& operator=(ContiguousView&&) = default; + constexpr friend int* begin(ContiguousView& view) { return view.ptr_; } + constexpr friend int* begin(ContiguousView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(ContiguousView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(ContiguousView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +struct CopyableView : std::ranges::view_base { + int *ptr_; + constexpr CopyableView(int* ptr) : ptr_(ptr) {} + constexpr friend int* begin(CopyableView& view) { return view.ptr_; } + constexpr friend int* begin(CopyableView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(CopyableView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(CopyableView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +constexpr bool hasLValueQualifiedBase(auto&& view) { + return requires { view.base(); }; +} + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + // TODO naming here + std::ranges::take_view tv(CopyableView{buffer}, 0); + assert(tv.base().ptr_ == buffer); + assert(std::move(tv).base().ptr_ == buffer); + + ASSERT_SAME_TYPE(decltype(tv.base()), CopyableView); + ASSERT_SAME_TYPE(decltype(std::move(tv).base()), CopyableView); + static_assert(hasLValueQualifiedBase(tv)); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 1); + assert(std::move(tv).base().ptr_ == buffer); + + ASSERT_SAME_TYPE(decltype(std::move(tv).base()), ContiguousView); + static_assert(!hasLValueQualifiedBase(tv)); + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 2); + assert(tv.base().ptr_ == buffer); + assert(std::move(tv).base().ptr_ == buffer); + + ASSERT_SAME_TYPE(decltype(tv.base()), CopyableView); + ASSERT_SAME_TYPE(decltype(std::move(tv).base()), CopyableView); + static_assert(hasLValueQualifiedBase(tv)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp @@ -0,0 +1,152 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// constexpr auto begin() requires (!simple-view); +// constexpr auto begin() const requires range; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct ContiguousView : std::ranges::view_base { + int *ptr_; + constexpr ContiguousView(int* ptr) : ptr_(ptr) {} + constexpr ContiguousView(ContiguousView&&) = default; + constexpr ContiguousView& operator=(ContiguousView&&) = default; + constexpr friend int* begin(ContiguousView& view) { return view.ptr_; } + constexpr friend int* begin(ContiguousView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(ContiguousView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(ContiguousView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +struct CopyableView : std::ranges::view_base { + int *ptr_; + constexpr CopyableView(int* ptr) : ptr_(ptr) {} + constexpr friend int* begin(CopyableView& view) { return view.ptr_; } + constexpr friend int* begin(CopyableView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(CopyableView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(CopyableView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +using ForwardIter = forward_iterator; +struct SizedForwardView : std::ranges::view_base { + int *ptr_; + constexpr SizedForwardView(int* ptr) : ptr_(ptr) {} + constexpr friend ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.ptr_); } + constexpr friend ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedForwardView& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedForwardView const& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedForwardView a sized view. +constexpr auto operator-(sentinel_wrapper sent, ForwardIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(ForwardIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +using RandomAccessIter = random_access_iterator; +struct SizedRandomAccessView : std::ranges::view_base { + int *ptr_; + constexpr SizedRandomAccessView(int* ptr) : ptr_(ptr) {} + constexpr friend RandomAccessIter begin(SizedRandomAccessView& view) { return RandomAccessIter(view.ptr_); } + constexpr friend RandomAccessIter begin(SizedRandomAccessView const& view) { return RandomAccessIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedRandomAccessView& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedRandomAccessView const& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedRandomAccessView a sized view. +constexpr auto operator-(sentinel_wrapper sent, RandomAccessIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(RandomAccessIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 4); + assert(tv.begin() == begin(SizedRandomAccessView(buffer))); + ASSERT_SAME_TYPE(decltype(tv.begin()), RandomAccessIter); + } + + { + const std::ranges::take_view tv(SizedRandomAccessView{buffer}, 4); + assert(tv.begin() == begin(SizedRandomAccessView(buffer))); + ASSERT_SAME_TYPE(decltype(tv.begin()), RandomAccessIter); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(ForwardIter(buffer), 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(ForwardIter(buffer), 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + const std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + std::ranges::take_view tv(CopyableView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// template +// inline constexpr bool enable_borrowed_range> = enable_borrowed_range; + +#include +#include + +#include "test_iterators.h" + +struct View : std::ranges::view_base { + friend int* begin(View&); + friend int* begin(View const&); + friend sentinel_wrapper end(View&); + friend sentinel_wrapper end(View const&); +}; + +struct BorrowableView : std::ranges::view_base { + friend int* begin(BorrowableView&); + friend int* begin(BorrowableView const&); + friend sentinel_wrapper end(BorrowableView&); + friend sentinel_wrapper end(BorrowableView const&); +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +static_assert(!std::ranges::enable_borrowed_range>); +static_assert( std::ranges::enable_borrowed_range>); diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/ctad.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/ctad.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/ctad.compile.pass.cpp @@ -0,0 +1,67 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// template +// take_view(R&&, range_difference_t) -> take_view>; + +#include +#include + +#include "test_iterators.h" + +struct View : std::ranges::view_base { + friend int* begin(View&); + friend int* begin(View const&); + friend sentinel_wrapper end(View&); + friend sentinel_wrapper end(View const&); +}; + +struct Range { + friend int* begin(Range&); + friend int* begin(Range const&); + friend sentinel_wrapper end(Range&); + friend sentinel_wrapper end(Range const&); +}; + +struct BorrowedRange { + friend int* begin(BorrowedRange&); + friend int* begin(BorrowedRange const&); + friend sentinel_wrapper end(BorrowedRange&); + friend sentinel_wrapper end(BorrowedRange const&); +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +void testCTAD() { + View v; + Range r; + BorrowedRange br; + static_assert(std::same_as< + decltype(std::ranges::take_view(v, 0)), + std::ranges::take_view + >); + static_assert(std::same_as< + decltype(std::ranges::take_view(r, 0)), + std::ranges::take_view> + >); + // std::ranges::take_view(std::move(r), 0) invalid. RValue range must be borrowed. + static_assert(std::same_as< + decltype(std::ranges::take_view(br, 0)), + std::ranges::take_view> + >); + static_assert(std::same_as< + decltype(std::ranges::take_view(std::move(br), 0)), + std::ranges::take_view, std::ranges::subrange_kind::unsized>> + >); +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/ctor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/ctor.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/ctor.pass.cpp @@ -0,0 +1,93 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// take_view() requires default_initializable = default; +// constexpr take_view(V base, range_difference_t count); + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct ContiguousView : std::ranges::view_base { + int *ptr_; + constexpr ContiguousView(int* ptr) : ptr_(ptr) {} + constexpr ContiguousView(ContiguousView&&) = default; + constexpr ContiguousView& operator=(ContiguousView&&) = default; + constexpr friend int* begin(ContiguousView& view) { return view.ptr_; } + constexpr friend int* begin(ContiguousView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(ContiguousView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(ContiguousView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +struct CopyableView : std::ranges::view_base { + int *ptr_; + constexpr CopyableView(int* ptr) : ptr_(ptr) {} + constexpr friend int* begin(CopyableView& view) { return view.ptr_; } + constexpr friend int* begin(CopyableView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(CopyableView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(CopyableView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +template +struct DefaultConstructible : std::ranges::view_base { + DefaultConstructible() requires IsDefaultCtorable = default; + DefaultConstructible(int*); + int* begin(); + sentinel_wrapper end(); +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + static_assert( std::default_initializable>); + static_assert(!std::default_initializable>); + } + + { + std::ranges::take_view tv(CopyableView{buffer}, 0); + assert(tv.base().ptr_ == buffer); + assert(tv.begin() == tv.end()); // Checking we have correct size. + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 1); + assert(std::move(tv).base().ptr_ == buffer); + assert(std::ranges::next(tv.begin(), 1) == tv.end()); // Checking we have correct size. + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 2); + assert(tv.base().ptr_ == buffer); + assert(std::ranges::next(tv.begin(), 2) == tv.end()); // Checking we have correct size. + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp @@ -0,0 +1,158 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// constexpr auto end() requires (!simple-view) +// constexpr auto end() const requires range + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct ContiguousView : std::ranges::view_base { + int *ptr_; + constexpr ContiguousView(int* ptr) : ptr_(ptr) {} + constexpr ContiguousView(ContiguousView&&) = default; + constexpr ContiguousView& operator=(ContiguousView&&) = default; + constexpr friend int* begin(ContiguousView& view) { return view.ptr_; } + constexpr friend int* begin(ContiguousView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(ContiguousView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(ContiguousView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +struct CopyableView : std::ranges::view_base { + int *ptr_; + constexpr CopyableView(int* ptr) : ptr_(ptr) {} + constexpr friend int* begin(CopyableView& view) { return view.ptr_; } + constexpr friend int* begin(CopyableView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(CopyableView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(CopyableView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +using ForwardIter = forward_iterator; +struct SizedForwardView : std::ranges::view_base { + int *ptr_; + constexpr SizedForwardView(int* ptr) : ptr_(ptr) {} + constexpr friend ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.ptr_); } + constexpr friend ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedForwardView& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedForwardView const& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedForwardView a sized view. +constexpr auto operator-(sentinel_wrapper sent, ForwardIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(ForwardIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +using RandomAccessIter = random_access_iterator; +struct SizedRandomAccessView : std::ranges::view_base { + int *ptr_; + constexpr SizedRandomAccessView(int* ptr) : ptr_(ptr) {} + constexpr friend RandomAccessIter begin(SizedRandomAccessView& view) { return RandomAccessIter(view.ptr_); } + constexpr friend RandomAccessIter begin(SizedRandomAccessView const& view) { return RandomAccessIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedRandomAccessView& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedRandomAccessView const& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedRandomAccessView a sized view. +constexpr auto operator-(sentinel_wrapper sent, RandomAccessIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(RandomAccessIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 0); + assert(tv.end() == std::ranges::next(tv.begin(), 0)); + ASSERT_SAME_TYPE(decltype(tv.end()), RandomAccessIter); + } + + { + const std::ranges::take_view tv(SizedRandomAccessView{buffer}, 1); + assert(tv.end() == std::ranges::next(tv.begin(), 1)); + ASSERT_SAME_TYPE(decltype(tv.end()), RandomAccessIter); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 2); + assert(tv.end() == std::ranges::next(tv.begin(), 2)); + ASSERT_SAME_TYPE(decltype(tv.end()), std::default_sentinel_t); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 3); + assert(tv.end() == std::ranges::next(tv.begin(), 3)); + ASSERT_SAME_TYPE(decltype(tv.end()), std::default_sentinel_t); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end() == std::ranges::next(tv.begin(), 4)); + + // The type. + static_assert(!std::same_as); + static_assert(!std::same_as); + } + + { + const std::ranges::take_view tv(ContiguousView{buffer}, 5); + assert(tv.end() == std::ranges::next(tv.begin(), 5)); + } + + { + std::ranges::take_view tv(CopyableView{buffer}, 6); + assert(tv.end() == std::ranges::next(tv.begin(), 6)); + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 7); + assert(tv.end() == std::ranges::next(tv.begin(), 7)); + } + + // Just to cover the case where count == 8. + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 8); + assert(tv.end() == std::ranges::next(tv.begin(), 8)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// sentinel() = default; +// constexpr explicit sentinel(sentinel_t end); +// constexpr sentinel(sentinel s) +// requires Const && convertible_to, sentinel_t>; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct ContiguousView : std::ranges::view_base { + int *ptr_; + constexpr ContiguousView(int* ptr) : ptr_(ptr) {} + constexpr ContiguousView(ContiguousView&&) = default; + constexpr ContiguousView& operator=(ContiguousView&&) = default; + constexpr friend int* begin(ContiguousView& view) { return view.ptr_; } + constexpr friend int* begin(ContiguousView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(ContiguousView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(ContiguousView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + auto sw = sentinel_wrapper(buffer + 8); // Note: not 4, but that's OK. + + { + const std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end().base().base() == sw.base()); + ASSERT_SAME_TYPE(decltype(tv.end().base()), sentinel_wrapper); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end().base().base() == sw.base()); + ASSERT_SAME_TYPE(decltype(tv.end().base()), sentinel_wrapper); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp @@ -0,0 +1,89 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// sentinel() = default; +// constexpr explicit sentinel(sentinel_t end); +// constexpr sentinel(sentinel s) +// requires Const && convertible_to, sentinel_t>; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct ContiguousView : std::ranges::view_base { + int *ptr_; + constexpr ContiguousView(int* ptr) : ptr_(ptr) {} + constexpr ContiguousView(ContiguousView&&) = default; + constexpr ContiguousView& operator=(ContiguousView&&) = default; + constexpr friend int* begin(ContiguousView& view) { return view.ptr_; } + constexpr friend int* begin(ContiguousView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(ContiguousView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(ContiguousView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +struct CopyableView : std::ranges::view_base { + int *ptr_; + constexpr CopyableView(int* ptr) : ptr_(ptr) {} + constexpr friend int* begin(CopyableView& view) { return view.ptr_; } + constexpr friend int* begin(CopyableView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(CopyableView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(CopyableView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + // Test the default ctor. + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(decltype(tv.end()){} == std::ranges::next(tv.begin(), 4)); + } + + { + std::ranges::take_view nonConst(ContiguousView{buffer}, 5); + const std::ranges::take_view tvConst(ContiguousView{buffer}, 5); + auto sent1 = nonConst.end(); + // Convert to const. Note, we cannot go the other way. + std::remove_cv_t sent2 = sent1; + + assert(sent1 == std::ranges::next(tvConst.begin(), 5)); + assert(sent2 == std::ranges::next(tvConst.begin(), 5)); + } + + { + std::ranges::take_view tv(CopyableView{buffer}, 6); + auto sw = sentinel_wrapper(buffer + 6); + using Sent = decltype(tv.end()); + Sent sent = Sent(sw); + assert(sent.base().base() == sw.base()); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/eq.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/eq.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/eq.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// sentinel() = default; +// constexpr explicit sentinel(sentinel_t end); +// constexpr sentinel(sentinel s) +// requires Const && convertible_to, sentinel_t>; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct ContiguousView : std::ranges::view_base { + int *ptr_; + constexpr ContiguousView(int* ptr) : ptr_(ptr) {} + constexpr ContiguousView(ContiguousView&&) = default; + constexpr ContiguousView& operator=(ContiguousView&&) = default; + constexpr friend int* begin(ContiguousView& view) { return view.ptr_; } + constexpr friend int* begin(ContiguousView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(ContiguousView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(ContiguousView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + { + const std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end() == std::ranges::next(tv.begin(), 4)); + assert(std::ranges::next(tv.begin(), 4) == tv.end()); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end() == std::ranges::next(tv.begin(), 4)); + assert(std::ranges::next(tv.begin(), 4) == tv.end()); + } + } + + { + std::ranges::take_view tvNonConst(ContiguousView{buffer}, 4); + const std::ranges::take_view tvConst(ContiguousView{buffer}, 4); + assert(tvNonConst.end() == std::ranges::next(tvConst.begin(), 4)); + assert(std::ranges::next(tvConst.begin(), 4) == tvNonConst.end()); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/size.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/size.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/size.pass.cpp @@ -0,0 +1,129 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// constexpr auto size() requires sized_range +// constexpr auto size() const requires sized_range + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +struct CopyableView : std::ranges::view_base { + int *ptr_; + constexpr CopyableView(int* ptr) : ptr_(ptr) {} + constexpr friend int* begin(CopyableView& view) { return view.ptr_; } + constexpr friend int* begin(CopyableView const& view) { return view.ptr_; } + constexpr friend sentinel_wrapper end(CopyableView& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } + constexpr friend sentinel_wrapper end(CopyableView const& view) { + return sentinel_wrapper{view.ptr_ + 8}; + } +}; + +using ForwardIter = forward_iterator; +struct SizedForwardView : std::ranges::view_base { + int *ptr_; + constexpr SizedForwardView(int* ptr) : ptr_(ptr) {} + constexpr friend ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.ptr_); } + constexpr friend ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedForwardView& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedForwardView const& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedForwardView a sized view. +constexpr auto operator-(sentinel_wrapper sent, ForwardIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(ForwardIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +using RandomAccessIter = random_access_iterator; +struct SizedRandomAccessView : std::ranges::view_base { + int *ptr_; + constexpr SizedRandomAccessView(int* ptr) : ptr_(ptr) {} + constexpr friend RandomAccessIter begin(SizedRandomAccessView& view) { return RandomAccessIter(view.ptr_); } + constexpr friend RandomAccessIter begin(SizedRandomAccessView const& view) { return RandomAccessIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedRandomAccessView& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedRandomAccessView const& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedRandomAccessView a sized view. +constexpr auto operator-(sentinel_wrapper sent, RandomAccessIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(RandomAccessIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +template +concept SizeEnabled = requires(const std::ranges::take_view& tv) { + tv.size(); +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + static_assert( SizeEnabled); + static_assert(!SizeEnabled); + } + + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 0); + assert(tv.size() == 0); + } + + { + const std::ranges::take_view tv(SizedRandomAccessView{buffer}, 2); + assert(tv.size() == 2); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 4); + assert(tv.size() == 4); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 6); + assert(tv.size() == 6); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 8); + assert(tv.size() == 8); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 10); + assert(tv.size() == 8); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +}