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