1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___RANGES_RBEGIN_H
11 #define _LIBCPP___RANGES_RBEGIN_H
12 
13 #include <__concepts/class_or_enum.h>
14 #include <__concepts/same_as.h>
15 #include <__config>
16 #include <__iterator/concepts.h>
17 #include <__iterator/readable_traits.h>
18 #include <__iterator/reverse_iterator.h>
19 #include <__ranges/access.h>
20 #include <__type_traits/decay.h>
21 #include <__type_traits/is_reference.h>
22 #include <__type_traits/remove_cvref.h>
23 #include <__type_traits/remove_reference.h>
24 #include <__utility/auto_cast.h>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_BEGIN_NAMESPACE_STD
31 
32 #if _LIBCPP_STD_VER >= 20
33 
34 // [ranges.access.rbegin]
35 
36 namespace ranges {
37 namespace __rbegin {
38 template <class _Tp>
39 concept __member_rbegin =
40   __can_borrow<_Tp> &&
41   __workaround_52970<_Tp> &&
42   requires(_Tp&& __t) {
43     { _LIBCPP_AUTO_CAST(__t.rbegin()) } -> input_or_output_iterator;
44   };
45 
46 void rbegin(auto&) = delete;
47 void rbegin(const auto&) = delete;
48 
49 template <class _Tp>
50 concept __unqualified_rbegin =
51   !__member_rbegin<_Tp> &&
52   __can_borrow<_Tp> &&
53   __class_or_enum<remove_cvref_t<_Tp>> &&
54   requires(_Tp&& __t) {
55     { _LIBCPP_AUTO_CAST(rbegin(__t)) } -> input_or_output_iterator;
56   };
57 
58 template <class _Tp>
59 concept __can_reverse =
60   __can_borrow<_Tp> &&
61   !__member_rbegin<_Tp> &&
62   !__unqualified_rbegin<_Tp> &&
63   requires(_Tp&& __t) {
64     { ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
65     { ranges::begin(__t) } -> bidirectional_iterator;
66   };
67 
68 struct __fn {
69   template <class _Tp>
70     requires __member_rbegin<_Tp>
71   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
72     noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rbegin())))
73   {
74     return _LIBCPP_AUTO_CAST(__t.rbegin());
75   }
76 
77   template <class _Tp>
78     requires __unqualified_rbegin<_Tp>
79   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
80     noexcept(noexcept(_LIBCPP_AUTO_CAST(rbegin(__t))))
81   {
82     return _LIBCPP_AUTO_CAST(rbegin(__t));
83   }
84 
85   template <class _Tp>
86     requires __can_reverse<_Tp>
87   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
88     noexcept(noexcept(ranges::end(__t)))
89   {
90     return std::make_reverse_iterator(ranges::end(__t));
91   }
92 
93   void operator()(auto&&) const = delete;
94 };
95 } // namespace __rbegin
96 
97 inline namespace __cpo {
98   inline constexpr auto rbegin = __rbegin::__fn{};
99 } // namespace __cpo
100 } // namespace ranges
101 
102 // [range.access.crbegin]
103 
104 namespace ranges {
105 namespace __crbegin {
106 struct __fn {
107   template <class _Tp>
108     requires is_lvalue_reference_v<_Tp&&>
109   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
110   constexpr auto operator()(_Tp&& __t) const
111     noexcept(noexcept(ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t))))
112     -> decltype(      ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t)))
113     { return          ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t)); }
114 
115   template <class _Tp>
116     requires is_rvalue_reference_v<_Tp&&>
117   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
118   constexpr auto operator()(_Tp&& __t) const
119     noexcept(noexcept(ranges::rbegin(static_cast<const _Tp&&>(__t))))
120     -> decltype(      ranges::rbegin(static_cast<const _Tp&&>(__t)))
121     { return          ranges::rbegin(static_cast<const _Tp&&>(__t)); }
122 };
123 } // namespace __crbegin
124 
125 inline namespace __cpo {
126   inline constexpr auto crbegin = __crbegin::__fn{};
127 } // namespace __cpo
128 } // namespace ranges
129 
130 #endif // _LIBCPP_STD_VER >= 20
131 
132 _LIBCPP_END_NAMESPACE_STD
133 
134 #endif // _LIBCPP___RANGES_RBEGIN_H
135