libstdc++
range_cmp.h
Go to the documentation of this file.
1 // Concept-constrained comparison implementations -*- C++ -*-
2 
3 // Copyright (C) 2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/range_cmp.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{functional}
28  */
29 
30 #ifndef _RANGE_CMP_H
31 #define _RANGE_CMP_H 1
32 
33 #if __cplusplus > 201703L
34 # include <bits/move.h>
35 # include <concepts>
36 
37 namespace std _GLIBCXX_VISIBILITY(default)
38 {
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 
41  struct __is_transparent; // not defined
42 
43  // Define std::identity here so that <iterator> and <ranges>
44  // don't need to include <bits/stl_function.h> to get it.
45 
46  /// [func.identity] The identity function.
47  struct identity
48  {
49  template<typename _Tp>
50  constexpr _Tp&&
51  operator()(_Tp&& __t) const noexcept
52  { return std::forward<_Tp>(__t); }
53 
54  using is_transparent = __is_transparent;
55  };
56 
57 #ifdef __cpp_lib_concepts
58 namespace ranges
59 {
60  namespace __detail
61  {
62  // BUILTIN-PTR-CMP(T, ==, U)
63  template<typename _Tp, typename _Up>
64  concept __eq_builtin_ptr_cmp
65  = convertible_to<_Tp, const volatile void*>
66  && convertible_to<_Up, const volatile void*>
67  && (! requires(_Tp&& __t, _Up&& __u)
68  { operator==(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
69  &&
70  ! requires(_Tp&& __t, _Up&& __u)
71  { std::forward<_Tp>(__t).operator==(std::forward<_Up>(__u)); });
72 
73  // BUILTIN-PTR-CMP(T, <, U)
74  template<typename _Tp, typename _Up>
75  concept __less_builtin_ptr_cmp
76  = convertible_to<_Tp, const volatile void*>
77  && convertible_to<_Up, const volatile void*>
78  && (! requires(_Tp&& __t, _Up&& __u)
79  { operator<(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
80  && ! requires(_Tp&& __t, _Up&& __u)
81  { std::forward<_Tp>(__t).operator<(std::forward<_Up>(__u)); });
82  } // namespace __detail
83 
84  // [range.cmp] Concept-constrained comparisons
85 
86  /// ranges::equal_to function object type.
87  struct equal_to
88  {
89  template<typename _Tp, typename _Up>
90  requires equality_comparable_with<_Tp, _Up>
91  || __detail::__eq_builtin_ptr_cmp<_Tp, _Up>
92  constexpr bool
93  operator()(_Tp&& __t, _Up&& __u) const
94  noexcept(noexcept(std::declval<_Tp>() == std::declval<_Up>()))
95  { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); }
96 
97  using is_transparent = __is_transparent;
98  };
99 
100  /// ranges::not_equal_to function object type.
101  struct not_equal_to
102  {
103  template<typename _Tp, typename _Up>
104  requires equality_comparable_with<_Tp, _Up>
105  || __detail::__eq_builtin_ptr_cmp<_Tp, _Up>
106  constexpr bool
107  operator()(_Tp&& __t, _Up&& __u) const
108  noexcept(noexcept(std::declval<_Up>() == std::declval<_Tp>()))
109  { return !equal_to{}(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
110 
111  using is_transparent = __is_transparent;
112  };
113 
114  /// ranges::less function object type.
115  struct less
116  {
117  template<typename _Tp, typename _Up>
118  requires totally_ordered_with<_Tp, _Up>
119  || __detail::__less_builtin_ptr_cmp<_Tp, _Up>
120  constexpr bool
121  operator()(_Tp&& __t, _Up&& __u) const
122  noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
123  {
124  if constexpr (__detail::__less_builtin_ptr_cmp<_Tp, _Up>)
125  {
126 #ifdef __cpp_lib_is_constant_evaluated
127  if (std::is_constant_evaluated())
128  return __t < __u;
129 #endif
130  auto __x = reinterpret_cast<__UINTPTR_TYPE__>(
131  static_cast<const volatile void*>(std::forward<_Tp>(__t)));
132  auto __y = reinterpret_cast<__UINTPTR_TYPE__>(
133  static_cast<const volatile void*>(std::forward<_Up>(__u)));
134  return __x < __y;
135  }
136  else
137  return std::forward<_Tp>(__t) < std::forward<_Up>(__u);
138  }
139 
140  using is_transparent = __is_transparent;
141  };
142 
143  /// ranges::greater function object type.
144  struct greater
145  {
146  template<typename _Tp, typename _Up>
147  requires totally_ordered_with<_Tp, _Up>
148  || __detail::__less_builtin_ptr_cmp<_Up, _Tp>
149  constexpr bool
150  operator()(_Tp&& __t, _Up&& __u) const
151  noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
152  { return less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
153 
154  using is_transparent = __is_transparent;
155  };
156 
157  /// ranges::greater_equal function object type.
158  struct greater_equal
159  {
160  template<typename _Tp, typename _Up>
161  requires totally_ordered_with<_Tp, _Up>
162  || __detail::__less_builtin_ptr_cmp<_Tp, _Up>
163  constexpr bool
164  operator()(_Tp&& __t, _Up&& __u) const
165  noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
166  { return !less{}(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
167 
168  using is_transparent = __is_transparent;
169  };
170 
171  /// ranges::less_equal function object type.
172  struct less_equal
173  {
174  template<typename _Tp, typename _Up>
175  requires totally_ordered_with<_Tp, _Up>
176  || __detail::__less_builtin_ptr_cmp<_Up, _Tp>
177  constexpr bool
178  operator()(_Tp&& __t, _Up&& __u) const
179  noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
180  { return !less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
181 
182  using is_transparent = __is_transparent;
183  };
184 
185 } // namespace ranges
186 #endif // library concepts
187 _GLIBCXX_END_NAMESPACE_VERSION
188 } // namespace std
189 #endif // C++20
190 #endif // _RANGE_CMP_H
std
ISO C++ entities toplevel namespace is std.
move.h
concepts