diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -52,6 +52,7 @@ __ranges/view_interface.h __ranges/view.h __ranges/size.h + __ranges/subrange.h __split_buffer __std_stream __string diff --git a/libcxx/include/__ranges/subrange.h b/libcxx/include/__ranges/subrange.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__ranges/subrange.h @@ -0,0 +1,240 @@ +// -*- 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_SUBRANGE_H +#define _LIBCPP___RANGES_SUBRANGE_H + +#include <__config> +#include <__iterator/concepts.h> +#include <__iterator/iterator_traits.h> +#include <__iterator/advance.h> +#include <__ranges/access.h> +#include <__ranges/enable_borrowed_range.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) + +// clang-format off +namespace ranges { + template + concept __convertible_to_non_slicing = + convertible_to<_From, _To> && + // If they're both pointers, they must have the same element type. + !(is_pointer_v> && + is_pointer_v> && + __not_same_as>, remove_pointer_t>>); + + template + concept __pair_like = + !is_reference_v<_Tp> && requires(_Tp __t) { + typename tuple_size<_Tp>::type; // Ensures `tuple_size` is complete. + requires derived_from, integral_constant>; + typename tuple_element_t<0, remove_const_t<_Tp>>; + typename tuple_element_t<1, remove_const_t<_Tp>>; + { get<0>(__t) } -> convertible_to&>; + { get<1>(__t) } -> convertible_to&>; + }; + + template + concept __pair_like_convertible_from = + !range<_Pair> && __pair_like<_Pair> && + constructible_from<_Pair, _Iter, _Sent> && + __convertible_to_non_slicing<_Iter, tuple_element_t<0, _Pair>> && + convertible_to<_Sent, tuple_element_t<1, _Pair>>; + + enum class _LIBCPP_ENUM_VIS subrange_kind : bool { unsized, sized }; + + template + struct __subrange_base { + static constexpr bool __store_size = false; + _Iter __begin = _Iter(); + _Sent __end = _Sent(); + + constexpr __subrange_base() = default; + constexpr __subrange_base(_Iter __iter, _Sent __sent) + : __begin(_VSTD::move(__iter)), __end(__sent) { } + }; + + template + struct __subrange_base<_Iter, _Sent, subrange_kind::sized> { + static constexpr bool __store_size = true; + _Iter __begin = _Iter(); + _Sent __end = _Sent(); + make_unsigned_t> __size = 0; + + constexpr __subrange_base() = default; + constexpr __subrange_base(_Iter __iter, _Sent __sent, decltype(__size) __size) + : __begin(_VSTD::move(__iter)), __end(__sent), __size(__size) { } + }; + + template _Sent = _Iter, + subrange_kind _Kind = sized_sentinel_for<_Sent, _Iter> + ? subrange_kind::sized + : subrange_kind::unsized> + requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _Iter>) + struct _LIBCPP_TEMPLATE_VIS subrange + : public view_interface>, + private __subrange_base<_Iter, _Sent, _Kind> { + + constexpr subrange() = default; + + using _Base = __subrange_base<_Iter, _Sent, _Kind>; + using _Base::__store_size; + using _Base::__begin; + using _Base::__end; + + constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent) + requires (!__store_size) + : _Base(_VSTD::move(__iter), __sent) {} + + constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent, + make_unsigned_t> __n) + requires (_Kind == subrange_kind::sized) + : _Base(_VSTD::move(__iter), __sent, __n) { } + + template<__not_same_as _Range> + requires borrowed_range<_Range> && + __convertible_to_non_slicing, _Iter> && + convertible_to, _Sent> + constexpr subrange(_Range&& __range) + requires (!__store_size) + : subrange(ranges::begin(__range), ranges::end(__range)) { } + + template<__not_same_as _Range> + requires borrowed_range<_Range> && + __convertible_to_non_slicing, _Iter> && + convertible_to, _Sent> + constexpr subrange(_Range&& __range) + requires __store_size && sized_range<_Range> + : subrange(__range, ranges::size(__range)) { } + + + template + requires __convertible_to_non_slicing, _Iter> && + convertible_to, _Sent> + constexpr subrange(_Range&& __range, make_unsigned_t> __n) + requires (_Kind == subrange_kind::sized) + : subrange(ranges::begin(__range), ranges::end(__range), __n) { } + + template<__not_same_as _Pair> + requires __pair_like_convertible_from<_Pair, const _Iter&, const _Sent&> + constexpr operator _Pair() const { return _Pair(__begin, __end); } + + constexpr _Iter begin() const requires copyable<_Iter> { + return __begin; + } + + [[nodiscard]] constexpr _Iter begin() requires (!copyable<_Iter>) { + return _VSTD::move(__begin); + } + + constexpr _Sent end() const { return __end; } + + [[nodiscard]] constexpr bool empty() const { return __begin == __end; } + + constexpr make_unsigned_t> size() const + requires (_Kind == subrange_kind::sized) + { + if constexpr (__store_size) + return _Base::__size; + else + return __to_unsigned_like(__end - __begin); + } + + [[nodiscard]] constexpr subrange next(iter_difference_t<_Iter> __n = 1) const& + requires forward_iterator<_Iter> { + auto __tmp = *this; + __tmp.advance(__n); + return __tmp; + } + + [[nodiscard]] constexpr subrange next(iter_difference_t<_Iter> __n = 1) && { + advance(__n); + return _VSTD::move(*this); + } + + [[nodiscard]] constexpr subrange prev(iter_difference_t<_Iter> __n = 1) const + requires bidirectional_iterator<_Iter> { + auto __tmp = *this; + __tmp.advance(-__n); + return __tmp; + } + + constexpr subrange& advance(iter_difference_t<_Iter> __n) { + if constexpr (bidirectional_iterator<_Iter>) { + if (__n < 0) { + ranges::advance(__begin, __n); + if constexpr (__store_size) + _Base::__size += __to_unsigned_like(-__n); + return *this; + } + } + + auto __d = __n - ranges::advance(__begin, __n, __end); + if constexpr (__store_size) + _Base::__size -= __to_unsigned_like(__d); + return *this; + } + }; + + template _Sent> + subrange(_Iter, _Sent) -> subrange<_Iter, _Sent>; + + template _Sent> + subrange(_Iter, _Sent, make_unsigned_t>) + -> subrange<_Iter, _Sent, subrange_kind::sized>; + + template + subrange(_Range&&) -> subrange, sentinel_t<_Range>, + (sized_range<_Range> || sized_sentinel_for, iterator_t<_Range>>) + ? subrange_kind::sized : subrange_kind::unsized>; + + template + subrange(_Range&&, make_unsigned_t>) -> + subrange, sentinel_t<_Range>, subrange_kind::sized>; + + template + requires (_Index < 2) + constexpr auto get(const subrange<_Iter, _Sent, _Kind>& __subrange) { + if constexpr (_Index == 0) + return __subrange.begin(); + else + return __subrange.end(); + } + + template + requires (_Index < 2) + constexpr auto get(subrange<_Iter, _Sent, _Kind>&& __subrange) { + if constexpr (_Index == 0) + return __subrange.begin(); + else + return __subrange.end(); + } +} // namespace ranges + +using ranges::get; + +// clang-format off + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___RANGES_SUBRANGE_H diff --git a/libcxx/include/concepts b/libcxx/include/concepts --- a/libcxx/include/concepts +++ b/libcxx/include/concepts @@ -442,6 +442,9 @@ template concept strict_weak_order = relation<_Rp, _Tp, _Up>; +template +concept __not_same_as = !same_as, remove_cvref_t<_Up>>; + #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS) _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/ranges b/libcxx/include/ranges --- a/libcxx/include/ranges +++ b/libcxx/include/ranges @@ -94,6 +94,7 @@ #include <__ranges/empty.h> #include <__ranges/enable_borrowed_range.h> #include <__ranges/size.h> +#include <__ranges/subrange.h> #include <__ranges/view_interface.h> #include <__ranges/view.h> #include // Required by the standard. diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/access.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/access.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/access.pass.cpp @@ -0,0 +1,180 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" + +namespace ranges = std::ranges; + +// Note: begin and end tested in range.subrange.ctor.pass.cpp. + +int globalBuff[8]; + +struct MoveOnlyForwardIter { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef std::ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef MoveOnlyForwardIter self; + + int *base = nullptr; + + MoveOnlyForwardIter() = default; + MoveOnlyForwardIter(MoveOnlyForwardIter &&) = default; + MoveOnlyForwardIter &operator=(MoveOnlyForwardIter&&) = default; + MoveOnlyForwardIter(MoveOnlyForwardIter const&) = delete; + constexpr MoveOnlyForwardIter(int *ptr) : base(ptr) { } + + friend bool operator==(const self&, const self&); + constexpr friend bool operator==(const self& lhs, int* rhs) { return lhs.base == rhs; } + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + constexpr operator pointer() const { return base; } +}; + +using ForwardIter = forward_iterator; + +using std::ptrdiff_t; +struct SizedSentinelForwardIter { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef std::make_unsigned_t udifference_type; + typedef SizedSentinelForwardIter self; + + int *base; + + SizedSentinelForwardIter() = default; + constexpr SizedSentinelForwardIter(int *ptr) : base(ptr) { } + + constexpr friend bool operator==(const self& lhs, const self& rhs) { return lhs.base == rhs.base; } + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + constexpr friend difference_type operator-(SizedSentinelForwardIter const&a, + SizedSentinelForwardIter const&b) { + return a.base - b.base; + } +}; + +constexpr bool testPrimatives() { + ranges::subrange a(MoveOnlyForwardIter(globalBuff), globalBuff + 8, 8); + assert(a.begin().base == globalBuff); + assert(!a.empty()); + assert(a.size() == 8); + + ranges::subrange b(ForwardIter(nullptr), ForwardIter(nullptr)); + assert(b.empty()); + + ranges::subrange c{ForwardIter(globalBuff), ForwardIter(globalBuff)}; + assert(c.empty()); + + ranges::subrange d(ForwardIter(globalBuff), ForwardIter(globalBuff + 1)); + assert(!d.empty()); + + ranges::subrange e(SizedSentinelForwardIter(globalBuff), + SizedSentinelForwardIter(globalBuff + 8), 8); + assert(!e.empty()); + assert(e.size() == 8); + + return true; +} + +using InputIter = cpp17_input_iterator; + +using BidirIter = bidirectional_iterator; + +constexpr bool testAdvance() { + ranges::subrange a(globalBuff, globalBuff + 8, 8); + auto a1 = a.next(); + assert(a1.begin() == globalBuff + 1); + assert(a1.size() == 7); + auto a5 = a.next(5); + assert(a5.begin() == globalBuff + 5); + assert(a5.size() == 3); + auto a4 = a5.prev(); + assert(a4.begin() == globalBuff + 4); + assert(a4.size() == 4); + + ranges::subrange b(InputIter(globalBuff), InputIter(globalBuff + 8)); + auto b1 = std::move(b).next(); + assert(b1.begin().base() == globalBuff + 1); + + ranges::subrange c(BidirIter(globalBuff + 4), BidirIter(globalBuff + 8)); + auto c1 = c.prev(); + assert(c1.begin().base() == globalBuff + 3); + auto c2 = c.prev(4); + assert(c2.begin().base() == globalBuff); + + ranges::subrange d(BidirIter(globalBuff + 4), BidirIter(globalBuff + 8)); + auto d1 = d.advance(4); + assert(d1.begin().base() == globalBuff + 8); + assert(d1.empty()); + auto d2 = d1.advance(-4); + assert(d2.begin().base() == globalBuff + 4); + + return true; +} + +template +concept GetInvocable = requires { + std::get(std::declval()); +}; + +static_assert( GetInvocable<0, ranges::subrange>); +static_assert( GetInvocable<1, ranges::subrange>); +static_assert(!GetInvocable<2, ranges::subrange>); +static_assert(!GetInvocable<3, ranges::subrange>); + +constexpr bool testGet() { + ranges::subrange a(globalBuff, globalBuff + 8, 8); + assert(std::get<0>(a) == a.begin()); + assert(std::get<1>(a) == a.end()); + + assert(a.begin() == std::get<0>(std::move(a))); + ranges::subrange b(globalBuff, globalBuff + 8, 8); + assert(b.end() == std::get<1>(std::move(b))); + + return true; +} + +int main(int, char**) { + testPrimatives(); + static_assert(testPrimatives()); + + testAdvance(); + static_assert(testAdvance()); + + testGet(); + static_assert(testGet()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctad.compile.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctad.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctad.compile.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" + +using std::ranges::subrange; + +static_assert(std::same_as(nullptr), forward_iterator(nullptr))), + subrange, forward_iterator, std::ranges::subrange_kind::unsized>>); +static_assert(std::same_as(nullptr), static_cast(nullptr), 0)), + subrange>); +static_assert(std::same_as(nullptr), nullptr, 0)), + subrange>); + +struct ForwardRange { + forward_iterator begin() const; + forward_iterator end() const; +}; +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +struct SizedRange { + int *begin(); + int *end(); +}; +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +static_assert(std::same_as, forward_iterator, std::ranges::subrange_kind::unsized>>); +static_assert(std::same_as>); +static_assert(std::same_as>); diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctor.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctor.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctor.pass.cpp @@ -0,0 +1,256 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// class std::ranges::subrange; + +#include + +#include +#include +#include "test_macros.h" +#include "test_iterators.h" + +namespace ranges = std::ranges; + +// convertible-to-non-slicing cases: +// 1. Not convertible (fail) +// 2. Only one is a pointer (succeed) +// 3. Both are not pointers (succeed) +// 4. Pointer elements are different types (fail) +// 5. Pointer elements are same type (succeed) + +int globalBuff[8]; + +struct Empty {}; + +using ForwardIter = forward_iterator; +using FowardSubrange = ranges::subrange; + +static_assert(std::is_default_constructible_v); + +struct ConvertibleForwardIter { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef std::ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef ConvertibleForwardIter self; + + int *base = nullptr; + + constexpr ConvertibleForwardIter() = default; + constexpr explicit ConvertibleForwardIter(int *ptr) : base(ptr) { } + + friend bool operator==(const self&, const self&); + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + constexpr operator pointer() const { return base; } + + // Explicitly deleted so this doesn't model sized_sentinel_for. + friend constexpr difference_type operator-(int *, self const&) = delete; + friend constexpr difference_type operator-(self const&, int*) = delete; +}; +using ConvertibleFowardSubrange = ranges::subrange; +static_assert(std::is_convertible_v); + +using std::ptrdiff_t; +template +struct SizedSentinelForwardIterBase { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef std::make_unsigned_t udifference_type; + typedef SizedSentinelForwardIterBase self; + + int *value = nullptr; + + constexpr SizedSentinelForwardIterBase() = default; + constexpr explicit SizedSentinelForwardIterBase(int *ptr) : value(ptr) {} + + constexpr int *base() const { return value; } + + friend bool operator==(const self&, const self&); + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + template + requires E + constexpr operator pointer() const { return value; } +}; +using SizedSentinelForwardIter = SizedSentinelForwardIterBase; +using SizedSentinelFowardSubrange = ranges::subrange; +using ConvertibleSizedSentinelForwardIter = SizedSentinelForwardIterBase; +using ConvertibleSizedSentinelForwardSubrange = ranges::subrange; + +using SizedIntPtrSubrange = ranges::subrange; + +// !StoreSize ctor. +static_assert( std::is_constructible_v); // Default case. +static_assert(!std::is_constructible_v); // 1. +static_assert( std::is_constructible_v); // 2. +static_assert( std::is_constructible_v); // 3. (Same as default case.) +// 4. and 5. must be sized. + +// Sized ctor. +static_assert( std::is_constructible_v); // Default case. +static_assert(!std::is_constructible_v); // 1. +static_assert( std::is_constructible_v); // 2. +static_assert( std::is_constructible_v); // 3. (Same as default case.) +static_assert(!std::is_constructible_v); // 4. +static_assert( std::is_constructible_v); // 5. +// TODO: is there any integral type that is not convertible to an unsized type? +// static_assert(!std::is_constructible_v); // Not unsigned. + +struct ForwardBorrowedRange { + constexpr ForwardIter begin() const { return ForwardIter(globalBuff); } + constexpr ForwardIter end() const { return ForwardIter(globalBuff + 8); } +}; + +template<> +inline constexpr bool ranges::enable_borrowed_range = true; + +struct ForwardRange { + ForwardIter begin() const; + ForwardIter end() const; +}; + +struct ConvertibleForwardBorrowedRange { + constexpr ConvertibleForwardIter begin() const { return ConvertibleForwardIter(globalBuff); } + constexpr int *end() const { return globalBuff + 8; } +}; + +template<> +inline constexpr bool ranges::enable_borrowed_range = true; + +struct ForwardBorrowedRangeDifferentSentienl { + struct sentinel { + int *value; + friend bool operator==(sentinel s, ForwardIter i) { return s.value == i.base(); } + }; + + constexpr ForwardIter begin() const { return ForwardIter(globalBuff); } + constexpr sentinel end() const { return sentinel{globalBuff + 8}; } +}; + +template<> +inline constexpr bool ranges::enable_borrowed_range = true; + +using DifferentSentienlSubrange = ranges::subrange; + +struct DifferentSentienlWithSizeMember { + struct sentinel { + int *value; + friend bool operator==(sentinel s, ForwardIter i) { return s.value == i.base(); } + }; + + constexpr ForwardIter begin() const { return ForwardIter(globalBuff); } + constexpr sentinel end() const { return sentinel{globalBuff + 8}; } + constexpr size_t size() const { return 8; } +}; + +template<> +inline constexpr bool ranges::enable_borrowed_range = true; + +using DifferentSentienlWithSizeMemberSubrange = ranges::subrange; + +// Range ctor. +static_assert( std::is_constructible_v); // Default case. +static_assert(!std::is_constructible_v); // Not borrowed. +// Iter convertible to sentinel (pointer) type. +static_assert( std::is_constructible_v); +// Where neither iter or sentinel are pointers, but they are different. +static_assert( std::is_constructible_v); +static_assert( std::is_constructible_v); + +// Pair like conversion. +static_assert( std::is_convertible_v>); +static_assert( std::is_convertible_v>); +static_assert(!std::is_convertible_v&>); +static_assert(!std::is_convertible_v>); +static_assert( std::is_convertible_v>); +static_assert(!std::is_convertible_v>); +static_assert( std::is_convertible_v>); + +constexpr bool test() { + FowardSubrange a(ForwardIter(globalBuff), ForwardIter(globalBuff + 8)); + assert(a.begin().base() == globalBuff); + assert(a.end().base() == globalBuff + 8); + + ConvertibleFowardSubrange b(ConvertibleForwardIter(globalBuff), globalBuff + 8); + assert(b.begin() == globalBuff); + assert(b.end() == globalBuff + 8); + + SizedSentinelFowardSubrange d(SizedSentinelForwardIter(globalBuff), SizedSentinelForwardIter(globalBuff + 8), 8); + assert(d.begin().base() == globalBuff); + assert(d.end().base() == globalBuff + 8); + assert(d.size() == 8); + + ConvertibleSizedSentinelForwardSubrange e(ConvertibleSizedSentinelForwardIter(globalBuff), ConvertibleSizedSentinelForwardIter(globalBuff + 8), 8); + assert(e.begin() == globalBuff); + assert(e.end() == globalBuff + 8); + assert(e.size() == 8); + + SizedIntPtrSubrange f(globalBuff, globalBuff + 8, 8); + assert(f.begin() == globalBuff); + assert(f.end() == globalBuff + 8); + assert(f.size() == 8); + + FowardSubrange g{ForwardBorrowedRange()}; + assert(g.begin().base() == globalBuff); + assert(g.end().base() == globalBuff + 8); + + ConvertibleFowardSubrange h{ConvertibleForwardBorrowedRange()}; + assert(h.begin() == globalBuff); + assert(h.end() == globalBuff + 8); + + DifferentSentienlSubrange i{ForwardBorrowedRangeDifferentSentienl()}; + assert(i.begin().base() == globalBuff); + assert(i.end().value == globalBuff + 8); + + FowardSubrange j(ForwardIter(globalBuff), ForwardIter(globalBuff + 8)); + std::pair jPair = j; + assert(jPair.first.base() == globalBuff); + assert(jPair.second.base() == globalBuff + 8); + std::tuple jTuple = j; + assert(get<0>(jTuple).base() == globalBuff); + assert(get<1>(jTuple).base() == globalBuff + 8); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/general.compile.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/general.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/general.compile.pass.cpp @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" + +namespace ranges = std::ranges; + +template +concept ValidSubrangeKind = requires { typename std::ranges::subrange; }; + +template +concept ValidSubrange = requires { typename std::ranges::subrange; }; + +static_assert( ValidSubrange>); +static_assert( ValidSubrange, forward_iterator>); +static_assert( ValidSubrangeKind, forward_iterator>); +static_assert( ValidSubrangeKind, forward_iterator>); +// Wrong sentinel type. +static_assert(!ValidSubrange, int*>); +static_assert( ValidSubrange); +static_assert( ValidSubrange); +// Must be sized. +static_assert(!ValidSubrangeKind); +static_assert( ValidSubrangeKind); +// Wrong sentinel type. +static_assert(!ValidSubrange>); +// Not an iterator. +static_assert(!ValidSubrange);