2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANGES_ACCESS_H
10 #define _LIBCPP___RANGES_ACCESS_H
12 #include <__concepts/class_or_enum.h>
14 #include <__iterator/concepts.h>
15 #include <__iterator/readable_traits.h>
16 #include <__ranges/enable_borrowed_range.h>
17 #include <__utility/auto_cast.h>
18 #include <type_traits>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 #if _LIBCPP_STD_VER > 17
30 concept __can_borrow =
31 is_lvalue_reference_v<_Tp> || enable_borrowed_range<remove_cvref_t<_Tp>>;
34 // [range.access.begin]
39 concept __member_begin =
41 __workaround_52970<_Tp> &&
43 { _LIBCPP_AUTO_CAST(__t.begin()) } -> input_or_output_iterator;
46 void begin(auto&) = delete;
47 void begin(const auto&) = delete;
50 concept __unqualified_begin =
51 !__member_begin<_Tp> &&
53 __class_or_enum<remove_cvref_t<_Tp>> &&
54 requires(_Tp && __t) {
55 { _LIBCPP_AUTO_CAST(begin(__t)) } -> input_or_output_iterator;
60 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp (&__t)[]) const noexcept
61 requires (sizeof(_Tp) >= 0) // Disallow incomplete element types.
66 template <class _Tp, size_t _Np>
67 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp (&__t)[_Np]) const noexcept
68 requires (sizeof(_Tp) >= 0) // Disallow incomplete element types.
74 requires __member_begin<_Tp>
75 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
76 noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.begin())))
78 return _LIBCPP_AUTO_CAST(__t.begin());
82 requires __unqualified_begin<_Tp>
83 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
84 noexcept(noexcept(_LIBCPP_AUTO_CAST(begin(__t))))
86 return _LIBCPP_AUTO_CAST(begin(__t));
89 void operator()(auto&&) const = delete;
91 } // namespace __begin
93 inline namespace __cpo {
94 inline constexpr auto begin = __begin::__fn{};
102 using iterator_t = decltype(ranges::begin(declval<_Tp&>()));
103 } // namespace ranges
105 // [range.access.end]
110 concept __member_end =
112 __workaround_52970<_Tp> &&
113 requires(_Tp&& __t) {
114 typename iterator_t<_Tp>;
115 { _LIBCPP_AUTO_CAST(__t.end()) } -> sentinel_for<iterator_t<_Tp>>;
118 void end(auto&) = delete;
119 void end(const auto&) = delete;
122 concept __unqualified_end =
123 !__member_end<_Tp> &&
125 __class_or_enum<remove_cvref_t<_Tp>> &&
126 requires(_Tp && __t) {
127 typename iterator_t<_Tp>;
128 { _LIBCPP_AUTO_CAST(end(__t)) } -> sentinel_for<iterator_t<_Tp>>;
132 template <class _Tp, size_t _Np>
133 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp (&__t)[_Np]) const noexcept
134 requires (sizeof(_Tp) >= 0) // Disallow incomplete element types.
140 requires __member_end<_Tp>
141 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
142 noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.end())))
144 return _LIBCPP_AUTO_CAST(__t.end());
148 requires __unqualified_end<_Tp>
149 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
150 noexcept(noexcept(_LIBCPP_AUTO_CAST(end(__t))))
152 return _LIBCPP_AUTO_CAST(end(__t));
155 void operator()(auto&&) const = delete;
159 inline namespace __cpo {
160 inline constexpr auto end = __end::__fn{};
162 } // namespace ranges
164 // [range.access.cbegin]
170 requires is_lvalue_reference_v<_Tp&&>
171 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
172 constexpr auto operator()(_Tp&& __t) const
173 noexcept(noexcept(ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t))))
174 -> decltype( ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t)))
175 { return ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t)); }
178 requires is_rvalue_reference_v<_Tp&&>
179 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
180 constexpr auto operator()(_Tp&& __t) const
181 noexcept(noexcept(ranges::begin(static_cast<const _Tp&&>(__t))))
182 -> decltype( ranges::begin(static_cast<const _Tp&&>(__t)))
183 { return ranges::begin(static_cast<const _Tp&&>(__t)); }
185 } // namespace __cbegin
187 inline namespace __cpo {
188 inline constexpr auto cbegin = __cbegin::__fn{};
190 } // namespace ranges
192 // [range.access.cend]
198 requires is_lvalue_reference_v<_Tp&&>
199 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
200 constexpr auto operator()(_Tp&& __t) const
201 noexcept(noexcept(ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t))))
202 -> decltype( ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t)))
203 { return ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t)); }
206 requires is_rvalue_reference_v<_Tp&&>
207 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
208 constexpr auto operator()(_Tp&& __t) const
209 noexcept(noexcept(ranges::end(static_cast<const _Tp&&>(__t))))
210 -> decltype( ranges::end(static_cast<const _Tp&&>(__t)))
211 { return ranges::end(static_cast<const _Tp&&>(__t)); }
213 } // namespace __cend
215 inline namespace __cpo {
216 inline constexpr auto cend = __cend::__fn{};
218 } // namespace ranges
220 #endif // _LIBCPP_STD_VER > 17
222 _LIBCPP_END_NAMESPACE_STD
224 #endif // _LIBCPP___RANGES_ACCESS_H