libstdc++
range_cmp.h
Go to the documentation of this file.
1 // Concept-constrained comparison implementations -*- C++ -*-
2 
3 // Copyright (C) 2019-2020 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 // Define this here, included by all the headers that need to define it.
59 #define __cpp_lib_ranges 201911L
60 
61 namespace ranges
62 {
63  namespace __detail
64  {
65  // BUILTIN-PTR-CMP(T, <, U)
66  // This determines whether t < u results in a call to a built-in operator<
67  // comparing pointers. It doesn't work for function pointers (PR 93628).
68  template<typename _Tp, typename _Up>
69  concept __less_builtin_ptr_cmp
70  = requires (_Tp&& __t, _Up&& __u) { { __t < __u } -> same_as<bool>; }
71  && convertible_to<_Tp, const volatile void*>
72  && convertible_to<_Up, const volatile void*>
73  && (! requires(_Tp&& __t, _Up&& __u)
74  { operator<(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
75  && ! requires(_Tp&& __t, _Up&& __u)
76  { std::forward<_Tp>(__t).operator<(std::forward<_Up>(__u)); });
77  } // namespace __detail
78 
79  // [range.cmp] Concept-constrained comparisons
80 
81  // _GLIBCXX_RESOLVE_LIB_DEFECTS
82  // 3530 BUILTIN-PTR-MEOW should not opt the type out of syntactic checks
83 
84  /// ranges::equal_to function object type.
85  struct equal_to
86  {
87  template<typename _Tp, typename _Up>
88  requires equality_comparable_with<_Tp, _Up>
89  constexpr bool
90  operator()(_Tp&& __t, _Up&& __u) const
91  noexcept(noexcept(std::declval<_Tp>() == std::declval<_Up>()))
92  { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); }
93 
94  using is_transparent = __is_transparent;
95  };
96 
97  /// ranges::not_equal_to function object type.
98  struct not_equal_to
99  {
100  template<typename _Tp, typename _Up>
101  requires equality_comparable_with<_Tp, _Up>
102  constexpr bool
103  operator()(_Tp&& __t, _Up&& __u) const
104  noexcept(noexcept(std::declval<_Up>() == std::declval<_Tp>()))
105  { return !equal_to{}(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
106 
107  using is_transparent = __is_transparent;
108  };
109 
110  /// ranges::less function object type.
111  struct less
112  {
113  template<typename _Tp, typename _Up>
114  requires totally_ordered_with<_Tp, _Up>
115  constexpr bool
116  operator()(_Tp&& __t, _Up&& __u) const
117  noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
118  {
119  if constexpr (__detail::__less_builtin_ptr_cmp<_Tp, _Up>)
120  {
121 #ifdef __cpp_lib_is_constant_evaluated
122  if (std::is_constant_evaluated())
123  return __t < __u;
124 #endif
125  auto __x = reinterpret_cast<__UINTPTR_TYPE__>(
126  static_cast<const volatile void*>(std::forward<_Tp>(__t)));
127  auto __y = reinterpret_cast<__UINTPTR_TYPE__>(
128  static_cast<const volatile void*>(std::forward<_Up>(__u)));
129  return __x < __y;
130  }
131  else
132  return std::forward<_Tp>(__t) < std::forward<_Up>(__u);
133  }
134 
135  using is_transparent = __is_transparent;
136  };
137 
138  /// ranges::greater function object type.
139  struct greater
140  {
141  template<typename _Tp, typename _Up>
142  requires totally_ordered_with<_Tp, _Up>
143  constexpr bool
144  operator()(_Tp&& __t, _Up&& __u) const
145  noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
146  { return less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
147 
148  using is_transparent = __is_transparent;
149  };
150 
151  /// ranges::greater_equal function object type.
152  struct greater_equal
153  {
154  template<typename _Tp, typename _Up>
155  requires totally_ordered_with<_Tp, _Up>
156  constexpr bool
157  operator()(_Tp&& __t, _Up&& __u) const
158  noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
159  { return !less{}(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
160 
161  using is_transparent = __is_transparent;
162  };
163 
164  /// ranges::less_equal function object type.
165  struct less_equal
166  {
167  template<typename _Tp, typename _Up>
168  requires totally_ordered_with<_Tp, _Up>
169  constexpr bool
170  operator()(_Tp&& __t, _Up&& __u) const
171  noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
172  { return !less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
173 
174  using is_transparent = __is_transparent;
175  };
176 
177 } // namespace ranges
178 #endif // library concepts
179 _GLIBCXX_END_NAMESPACE_VERSION
180 } // namespace std
181 #endif // C++20
182 #endif // _RANGE_CMP_H
ISO C++ entities toplevel namespace is std.