libstdc++
ranges_cmp.h
Go to the documentation of this file.
1 // Concept-constrained comparison implementations -*- C++ -*-
2 
3 // Copyright (C) 2019-2026 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/ranges_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 _RANGES_CMP_H
31 #define _RANGES_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 #pragma GCC diagnostic push
50 #pragma GCC diagnostic ignored "-Wc++23-extensions" // static operator()
51  template<typename _Tp>
52  [[nodiscard]]
53  static constexpr _Tp&&
54  operator()(_Tp&& __t) noexcept
55  { return std::forward<_Tp>(__t); }
56 #pragma GCC diagnostic pop
57 
58  using is_transparent = __is_transparent;
59  };
60 
61 #ifdef __glibcxx_ranges // C++ >= 20
62 namespace ranges
63 {
64  namespace __detail
65  {
66  // BUILTIN-PTR-CMP(T, <, U)
67  // This determines whether t < u results in a call to a built-in operator<
68  // comparing pointers. It doesn't work for function pointers (PR 93628).
69  template<typename _Tp, typename _Up>
70  concept __less_builtin_ptr_cmp
71  = requires (_Tp&& __t, _Up&& __u) { { __t < __u } -> same_as<bool>; }
72  && convertible_to<_Tp, const volatile void*>
73  && convertible_to<_Up, const volatile void*>
74  && ! requires(_Tp&& __t, _Up&& __u)
75  { operator<(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
76  && ! requires(_Tp&& __t, _Up&& __u)
77  { std::forward<_Tp>(__t).operator<(std::forward<_Up>(__u)); }
78  && std::__detail::__not_overloaded_spaceship<_Tp, _Up>;
79  } // namespace __detail
80 
81  // [range.cmp] Concept-constrained comparisons
82 
83  // _GLIBCXX_RESOLVE_LIB_DEFECTS
84  // 3530 BUILTIN-PTR-MEOW should not opt the type out of syntactic checks
85 
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Wc++23-extensions" // static operator()
88  /// ranges::equal_to function object type.
89  struct equal_to
90  {
91  template<typename _Tp, typename _Up>
92  requires equality_comparable_with<_Tp, _Up>
93  static constexpr bool
94  operator()(_Tp&& __t, _Up&& __u)
95  noexcept(noexcept(std::declval<_Tp>() == std::declval<_Up>()))
96  { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); }
97 
98  using is_transparent = __is_transparent;
99  };
100 
101  /// ranges::not_equal_to function object type.
103  {
104  template<typename _Tp, typename _Up>
105  requires equality_comparable_with<_Tp, _Up>
106  static constexpr bool
107  operator()(_Tp&& __t, _Up&& __u)
108  noexcept(noexcept(std::declval<_Tp>() == std::declval<_Up>()))
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  static constexpr bool
120  operator()(_Tp&& __t, _Up&& __u)
121  noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
122  {
123  if constexpr (__detail::__less_builtin_ptr_cmp<_Tp, _Up>)
124  {
125  if (std::__is_constant_evaluated())
126  return __t < __u;
127 
128  auto __x = reinterpret_cast<__UINTPTR_TYPE__>(
129  static_cast<const volatile void*>(std::forward<_Tp>(__t)));
130  auto __y = reinterpret_cast<__UINTPTR_TYPE__>(
131  static_cast<const volatile void*>(std::forward<_Up>(__u)));
132  return __x < __y;
133  }
134  else
135  return std::forward<_Tp>(__t) < std::forward<_Up>(__u);
136  }
137 
138  using is_transparent = __is_transparent;
139  };
140 
141  /// ranges::greater function object type.
142  struct greater
143  {
144  template<typename _Tp, typename _Up>
145  requires totally_ordered_with<_Tp, _Up>
146  static constexpr bool
147  operator()(_Tp&& __t, _Up&& __u)
148  noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
149  { return less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
150 
151  using is_transparent = __is_transparent;
152  };
153 
154  /// ranges::greater_equal function object type.
156  {
157  template<typename _Tp, typename _Up>
158  requires totally_ordered_with<_Tp, _Up>
159  static constexpr bool
160  operator()(_Tp&& __t, _Up&& __u)
161  noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
162  { return !less{}(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
163 
164  using is_transparent = __is_transparent;
165  };
166 
167  /// ranges::less_equal function object type.
168  struct less_equal
169  {
170  template<typename _Tp, typename _Up>
171  requires totally_ordered_with<_Tp, _Up>
172  static constexpr bool
173  operator()(_Tp&& __t, _Up&& __u)
174  noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
175  { return !less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
176 
177  using is_transparent = __is_transparent;
178  };
179 #pragma GCC diagnostic pop
180 
181 } // namespace ranges
182 #endif // __glibcxx_ranges
183 _GLIBCXX_END_NAMESPACE_VERSION
184 } // namespace std
185 #endif // C++20
186 #endif // _RANGES_CMP_H
[func.identity] The identity function.
Definition: ranges_cmp.h:47
ranges::greater_equal function object type.
Definition: ranges_cmp.h:155
ISO C++ entities toplevel namespace is std.
ranges::not_equal_to function object type.
Definition: ranges_cmp.h:102
ranges::greater function object type.
Definition: ranges_cmp.h:142
Definition: simd.h:306
ranges::equal_to function object type.
Definition: ranges_cmp.h:89
ranges::less_equal function object type.
Definition: ranges_cmp.h:168
ranges::less function object type.
Definition: ranges_cmp.h:115