libstdc++
concepts
Go to the documentation of this file.
1 // <concepts> -*- C++ -*-
2 
3 // Copyright (C) 2019-2024 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 include/concepts
26  * This is a Standard C++ Library header.
27  * @ingroup concepts
28  */
29 
30 #ifndef _GLIBCXX_CONCEPTS
31 #define _GLIBCXX_CONCEPTS 1
32 
33 #pragma GCC system_header
34 
35 #define __glibcxx_want_concepts
36 #include <bits/version.h>
37 
38 #ifdef __cpp_lib_concepts // C++ >= 20 && concepts
39 /**
40  * @defgroup concepts Concepts
41  * @ingroup utilities
42  *
43  * Concepts for checking type requirements.
44  */
45 
46 #include <type_traits>
47 
48 namespace std _GLIBCXX_VISIBILITY(default)
49 {
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 
52  // [concepts.lang], language-related concepts
53 
54  namespace __detail
55  {
56  template<typename _Tp, typename _Up>
57  concept __same_as = std::is_same_v<_Tp, _Up>;
58  } // namespace __detail
59 
60  /// [concept.same], concept same_as
61  template<typename _Tp, typename _Up>
62  concept same_as
63  = __detail::__same_as<_Tp, _Up> && __detail::__same_as<_Up, _Tp>;
64 
65  namespace __detail
66  {
67  template<typename _Tp, typename _Up>
68  concept __different_from
69  = !same_as<remove_cvref_t<_Tp>, remove_cvref_t<_Up>>;
70  } // namespace __detail
71 
72  /// [concept.derived], concept derived_from
73  template<typename _Derived, typename _Base>
74  concept derived_from = __is_base_of(_Base, _Derived)
75  && is_convertible_v<const volatile _Derived*, const volatile _Base*>;
76 
77  /// [concept.convertible], concept convertible_to
78  template<typename _From, typename _To>
79  concept convertible_to = is_convertible_v<_From, _To>
80  && requires { static_cast<_To>(std::declval<_From>()); };
81 
82  /// [concept.commonref], concept common_reference_with
83  template<typename _Tp, typename _Up>
84  concept common_reference_with
85  = same_as<common_reference_t<_Tp, _Up>, common_reference_t<_Up, _Tp>>
86  && convertible_to<_Tp, common_reference_t<_Tp, _Up>>
87  && convertible_to<_Up, common_reference_t<_Tp, _Up>>;
88 
89  /// [concept.common], concept common_with
90  template<typename _Tp, typename _Up>
91  concept common_with
92  = same_as<common_type_t<_Tp, _Up>, common_type_t<_Up, _Tp>>
93  && requires {
94  static_cast<common_type_t<_Tp, _Up>>(std::declval<_Tp>());
95  static_cast<common_type_t<_Tp, _Up>>(std::declval<_Up>());
96  }
97  && common_reference_with<add_lvalue_reference_t<const _Tp>,
98  add_lvalue_reference_t<const _Up>>
99  && common_reference_with<add_lvalue_reference_t<common_type_t<_Tp, _Up>>,
100  common_reference_t<
101  add_lvalue_reference_t<const _Tp>,
102  add_lvalue_reference_t<const _Up>>>;
103 
104  // [concepts.arithmetic], arithmetic concepts
105 
106  template<typename _Tp>
107  concept integral = is_integral_v<_Tp>;
108 
109  template<typename _Tp>
110  concept signed_integral = integral<_Tp> && is_signed_v<_Tp>;
111 
112  template<typename _Tp>
113  concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>;
114 
115  template<typename _Tp>
116  concept floating_point = is_floating_point_v<_Tp>;
117 
118  namespace __detail
119  {
120  template<typename _Tp>
121  using __cref = const remove_reference_t<_Tp>&;
122 
123  template<typename _Tp>
124  concept __class_or_enum
125  = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;
126 
127  template<typename _Tp>
128  constexpr bool __destructible_impl = false;
129  template<typename _Tp>
130  requires requires(_Tp& __t) { { __t.~_Tp() } noexcept; }
131  constexpr bool __destructible_impl<_Tp> = true;
132 
133  template<typename _Tp>
134  constexpr bool __destructible = __destructible_impl<_Tp>;
135  template<typename _Tp>
136  constexpr bool __destructible<_Tp&> = true;
137  template<typename _Tp>
138  constexpr bool __destructible<_Tp&&> = true;
139  template<typename _Tp, size_t _Nm>
140  constexpr bool __destructible<_Tp[_Nm]> = __destructible<_Tp>;
141 
142  } // namespace __detail
143 
144  /// [concept.assignable], concept assignable_from
145  template<typename _Lhs, typename _Rhs>
146  concept assignable_from
147  = is_lvalue_reference_v<_Lhs>
148  && common_reference_with<__detail::__cref<_Lhs>, __detail::__cref<_Rhs>>
149  && requires(_Lhs __lhs, _Rhs&& __rhs) {
150  { __lhs = static_cast<_Rhs&&>(__rhs) } -> same_as<_Lhs>;
151  };
152 
153  /// [concept.destructible], concept destructible
154  template<typename _Tp>
155  concept destructible = __detail::__destructible<_Tp>;
156 
157  /// [concept.constructible], concept constructible_from
158  template<typename _Tp, typename... _Args>
159  concept constructible_from
160  = destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
161 
162  /// [concept.defaultinitializable], concept default_initializable
163  template<typename _Tp>
164  concept default_initializable = constructible_from<_Tp>
165  && requires
166  {
167  _Tp{};
168  (void) ::new _Tp;
169  };
170 
171  /// [concept.moveconstructible], concept move_constructible
172  template<typename _Tp>
173  concept move_constructible
174  = constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
175 
176  /// [concept.copyconstructible], concept copy_constructible
177  template<typename _Tp>
178  concept copy_constructible
179  = move_constructible<_Tp>
180  && constructible_from<_Tp, _Tp&> && convertible_to<_Tp&, _Tp>
181  && constructible_from<_Tp, const _Tp&> && convertible_to<const _Tp&, _Tp>
182  && constructible_from<_Tp, const _Tp> && convertible_to<const _Tp, _Tp>;
183 
184  // [concept.swappable], concept swappable
185 
186  namespace ranges
187  {
188  /// @cond undocumented
189  namespace __swap
190  {
191  template<typename _Tp> void swap(_Tp&, _Tp&) = delete;
192 
193  template<typename _Tp, typename _Up>
194  concept __adl_swap
195  = (std::__detail::__class_or_enum<remove_reference_t<_Tp>>
196  || std::__detail::__class_or_enum<remove_reference_t<_Up>>)
197  && requires(_Tp&& __t, _Up&& __u) {
198  swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
199  };
200 
201  struct _Swap
202  {
203  private:
204  template<typename _Tp, typename _Up>
205  static constexpr bool
206  _S_noexcept()
207  {
208  if constexpr (__adl_swap<_Tp, _Up>)
209  return noexcept(swap(std::declval<_Tp>(), std::declval<_Up>()));
210  else
211  return is_nothrow_move_constructible_v<remove_reference_t<_Tp>>
212  && is_nothrow_move_assignable_v<remove_reference_t<_Tp>>;
213  }
214 
215  public:
216  template<typename _Tp, typename _Up>
217  requires __adl_swap<_Tp, _Up>
218  || (same_as<_Tp, _Up> && is_lvalue_reference_v<_Tp>
219  && move_constructible<remove_reference_t<_Tp>>
220  && assignable_from<_Tp, remove_reference_t<_Tp>>)
221  constexpr void
222  operator()(_Tp&& __t, _Up&& __u) const
223  noexcept(_S_noexcept<_Tp, _Up>())
224  {
225  if constexpr (__adl_swap<_Tp, _Up>)
226  swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
227  else
228  {
229  auto __tmp = static_cast<remove_reference_t<_Tp>&&>(__t);
230  __t = static_cast<remove_reference_t<_Tp>&&>(__u);
231  __u = static_cast<remove_reference_t<_Tp>&&>(__tmp);
232  }
233  }
234 
235  template<typename _Tp, typename _Up, size_t _Num>
236  requires requires(const _Swap& __swap, _Tp& __e1, _Up& __e2) {
237  __swap(__e1, __e2);
238  }
239  constexpr void
240  operator()(_Tp (&__e1)[_Num], _Up (&__e2)[_Num]) const
241  noexcept(noexcept(std::declval<const _Swap&>()(*__e1, *__e2)))
242  {
243  for (size_t __n = 0; __n < _Num; ++__n)
244  (*this)(__e1[__n], __e2[__n]);
245  }
246  };
247  } // namespace __swap
248  /// @endcond
249 
250  inline namespace _Cpo {
251  inline constexpr __swap::_Swap swap{};
252  }
253  } // namespace ranges
254 
255  template<typename _Tp>
256  concept swappable
257  = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
258 
259  template<typename _Tp, typename _Up>
260  concept swappable_with = common_reference_with<_Tp, _Up>
261  && requires(_Tp&& __t, _Up&& __u) {
262  ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Tp&&>(__t));
263  ranges::swap(static_cast<_Up&&>(__u), static_cast<_Up&&>(__u));
264  ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
265  ranges::swap(static_cast<_Up&&>(__u), static_cast<_Tp&&>(__t));
266  };
267 
268  // [concepts.object], Object concepts
269 
270  template<typename _Tp>
271  concept movable = is_object_v<_Tp> && move_constructible<_Tp>
272  && assignable_from<_Tp&, _Tp> && swappable<_Tp>;
273 
274  template<typename _Tp>
275  concept copyable = copy_constructible<_Tp> && movable<_Tp>
276  && assignable_from<_Tp&, _Tp&> && assignable_from<_Tp&, const _Tp&>
277  && assignable_from<_Tp&, const _Tp>;
278 
279  template<typename _Tp>
280  concept semiregular = copyable<_Tp> && default_initializable<_Tp>;
281 
282  // [concepts.compare], comparison concepts
283 
284  // [concept.booleantestable], Boolean testability
285  namespace __detail
286  {
287  template<typename _Tp>
288  concept __boolean_testable_impl = convertible_to<_Tp, bool>;
289 
290  template<typename _Tp>
291  concept __boolean_testable
292  = __boolean_testable_impl<_Tp>
293  && requires(_Tp&& __t)
294  { { !static_cast<_Tp&&>(__t) } -> __boolean_testable_impl; };
295  } // namespace __detail
296 
297  // [concept.equalitycomparable], concept equality_comparable
298 
299  namespace __detail
300  {
301  template<typename _Tp, typename _Up>
302  concept __weakly_eq_cmp_with
303  = requires(__detail::__cref<_Tp> __t, __detail::__cref<_Up> __u) {
304  { __t == __u } -> __boolean_testable;
305  { __t != __u } -> __boolean_testable;
306  { __u == __t } -> __boolean_testable;
307  { __u != __t } -> __boolean_testable;
308  };
309  } // namespace __detail
310 
311  template<typename _Tp>
312  concept equality_comparable = __detail::__weakly_eq_cmp_with<_Tp, _Tp>;
313 
314  template<typename _Tp, typename _Up>
315  concept equality_comparable_with
316  = equality_comparable<_Tp> && equality_comparable<_Up>
317  && common_reference_with<__detail::__cref<_Tp>, __detail::__cref<_Up>>
318  && equality_comparable<common_reference_t<__detail::__cref<_Tp>,
319  __detail::__cref<_Up>>>
320  && __detail::__weakly_eq_cmp_with<_Tp, _Up>;
321 
322  namespace __detail
323  {
324  template<typename _Tp, typename _Up>
325  concept __partially_ordered_with
326  = requires(const remove_reference_t<_Tp>& __t,
327  const remove_reference_t<_Up>& __u) {
328  { __t < __u } -> __boolean_testable;
329  { __t > __u } -> __boolean_testable;
330  { __t <= __u } -> __boolean_testable;
331  { __t >= __u } -> __boolean_testable;
332  { __u < __t } -> __boolean_testable;
333  { __u > __t } -> __boolean_testable;
334  { __u <= __t } -> __boolean_testable;
335  { __u >= __t } -> __boolean_testable;
336  };
337  } // namespace __detail
338 
339  // [concept.totallyordered], concept totally_ordered
340  template<typename _Tp>
341  concept totally_ordered
342  = equality_comparable<_Tp>
343  && __detail::__partially_ordered_with<_Tp, _Tp>;
344 
345  template<typename _Tp, typename _Up>
346  concept totally_ordered_with
347  = totally_ordered<_Tp> && totally_ordered<_Up>
348  && equality_comparable_with<_Tp, _Up>
349  && totally_ordered<common_reference_t<__detail::__cref<_Tp>,
350  __detail::__cref<_Up>>>
351  && __detail::__partially_ordered_with<_Tp, _Up>;
352 
353  template<typename _Tp>
354  concept regular = semiregular<_Tp> && equality_comparable<_Tp>;
355 
356  // [concepts.callable], callable concepts
357 
358  /// [concept.invocable], concept invocable
359  template<typename _Fn, typename... _Args>
360  concept invocable = is_invocable_v<_Fn, _Args...>;
361 
362  /// [concept.regularinvocable], concept regular_invocable
363  template<typename _Fn, typename... _Args>
364  concept regular_invocable = invocable<_Fn, _Args...>;
365 
366  /// [concept.predicate], concept predicate
367  template<typename _Fn, typename... _Args>
368  concept predicate = regular_invocable<_Fn, _Args...>
369  && __detail::__boolean_testable<invoke_result_t<_Fn, _Args...>>;
370 
371  /// [concept.relation], concept relation
372  template<typename _Rel, typename _Tp, typename _Up>
373  concept relation
374  = predicate<_Rel, _Tp, _Tp> && predicate<_Rel, _Up, _Up>
375  && predicate<_Rel, _Tp, _Up> && predicate<_Rel, _Up, _Tp>;
376 
377  /// [concept.equiv], concept equivalence_relation
378  template<typename _Rel, typename _Tp, typename _Up>
379  concept equivalence_relation = relation<_Rel, _Tp, _Up>;
380 
381  /// [concept.strictweakorder], concept strict_weak_order
382  template<typename _Rel, typename _Tp, typename _Up>
383  concept strict_weak_order = relation<_Rel, _Tp, _Up>;
384 
385 _GLIBCXX_END_NAMESPACE_VERSION
386 } // namespace
387 #endif // __cpp_lib_concepts
388 
389 #endif /* _GLIBCXX_CONCEPTS */