xref: /netbsd-src/external/apache2/llvm/dist/libcxx/include/concepts (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1// -*- C++ -*-
2//===-------------------------- concepts ----------------------------------===//
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
10#ifndef _LIBCPP_CONCEPTS
11#define _LIBCPP_CONCEPTS
12
13/*
14    concepts synopsis
15namespace std {
16  // [concepts.lang], language-related concepts
17  // [concept.same], concept same_as
18  template<class T, class U>
19    concept same_as = see below;
20
21  // [concept.derived], concept derived_from
22  template<class Derived, class Base>
23    concept derived_from = see below;
24
25  // [concept.convertible], concept convertible_to
26  template<class From, class To>
27    concept convertible_to = see below;
28
29  // [concept.commonref], concept common_reference_with
30  template<class T, class U>
31    concept common_reference_with = see below;
32
33  // [concept.common], concept common_with
34  template<class T, class U>
35    concept common_with = see below;
36
37  // [concepts.arithmetic], arithmetic concepts
38  template<class T>
39    concept integral = see below;
40  template<class T>
41    concept signed_integral = see below;
42  template<class T>
43    concept unsigned_integral = see below;
44  template<class T>
45    concept floating_point = see below;
46
47  // [concept.assignable], concept assignable_from
48  template<class LHS, class RHS>
49    concept assignable_from = see below;
50
51  // [concept.swappable], concept swappable
52  namespace ranges {
53    inline namespace unspecified {
54      inline constexpr unspecified swap = unspecified;
55    }
56  }
57  template<class T>
58    concept swappable = see below;
59  template<class T, class U>
60    concept swappable_with = see below;
61
62  // [concept.destructible], concept destructible
63  template<class T>
64    concept destructible = see below;
65
66  // [concept.constructible], concept constructible_from
67  template<class T, class... Args>
68    concept constructible_from = see below;
69
70  // [concept.default.init], concept default_initializable
71  template<class T>
72    concept default_initializable = see below;
73
74  // [concept.moveconstructible], concept move_constructible
75  template<class T>
76    concept move_constructible = see below;
77
78  // [concept.copyconstructible], concept copy_constructible
79  template<class T>
80    concept copy_constructible = see below;
81
82  // [concept.equalitycomparable], concept equality_comparable
83  template<class T>
84    concept equality_comparable = see below;
85  template<class T, class U>
86    concept equality_comparable_with = see below;
87
88  // [concept.totallyordered], concept totally_ordered
89  template<class T>
90    concept totally_ordered = see below;
91  template<class T, class U>
92    concept totally_ordered_with = see below;
93
94  // [concepts.object], object concepts
95  template<class T>
96    concept movable = see below;
97  template<class T>
98    concept copyable = see below;
99  template<class T>
100    concept semiregular = see below;
101  template<class T>
102    concept regular = see below;
103
104  // [concepts.callable], callable concepts
105  // [concept.invocable], concept invocable
106  template<class F, class... Args>
107    concept invocable = see below;
108
109  // [concept.regularinvocable], concept regular_invocable
110  template<class F, class... Args>
111    concept regular_invocable = see below;
112
113  // [concept.predicate], concept predicate
114  template<class F, class... Args>
115    concept predicate = see below;
116
117  // [concept.relation], concept relation
118  template<class R, class T, class U>
119    concept relation = see below;
120
121  // [concept.equiv], concept equivalence_relation
122  template<class R, class T, class U>
123    concept equivalence_relation = see below;
124
125  // [concept.strictweakorder], concept strict_weak_order
126  template<class R, class T, class U>
127    concept strict_weak_order = see below;
128}
129
130*/
131
132#include <__config>
133#include <__functional_base>
134#include <type_traits>
135#include <utility>
136#include <version>
137
138#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
139#pragma GCC system_header
140#endif
141
142_LIBCPP_PUSH_MACROS
143#include <__undef_macros>
144
145_LIBCPP_BEGIN_NAMESPACE_STD
146
147#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
148
149// [concept.same]
150
151template<class _Tp, class _Up>
152concept __same_as_impl = _VSTD::_IsSame<_Tp, _Up>::value;
153
154template<class _Tp, class _Up>
155concept same_as = __same_as_impl<_Tp, _Up> && __same_as_impl<_Up, _Tp>;
156
157// [concept.derived]
158template<class _Dp, class _Bp>
159concept derived_from =
160  is_base_of_v<_Bp, _Dp> &&
161  is_convertible_v<const volatile _Dp*, const volatile _Bp*>;
162
163// [concept.convertible]
164template<class _From, class _To>
165concept convertible_to =
166  is_convertible_v<_From, _To> &&
167  requires(add_rvalue_reference_t<_From> (&__f)()) {
168    static_cast<_To>(__f());
169  };
170
171// [concept.commonref]
172template<class _Tp, class _Up>
173concept common_reference_with =
174  same_as<common_reference_t<_Tp, _Up>, common_reference_t<_Up, _Tp>> &&
175  convertible_to<_Tp, common_reference_t<_Tp, _Up>> &&
176  convertible_to<_Up, common_reference_t<_Tp, _Up>>;
177
178// [concept.common]
179template<class _Tp, class _Up>
180concept common_with =
181  same_as<common_type_t<_Tp, _Up>, common_type_t<_Up, _Tp>> &&
182  requires {
183    static_cast<common_type_t<_Tp, _Up>>(declval<_Tp>());
184    static_cast<common_type_t<_Tp, _Up>>(declval<_Up>());
185  } &&
186  common_reference_with<
187    add_lvalue_reference_t<const _Tp>,
188    add_lvalue_reference_t<const _Up>> &&
189  common_reference_with<
190    add_lvalue_reference_t<common_type_t<_Tp, _Up>>,
191    common_reference_t<
192      add_lvalue_reference_t<const _Tp>,
193      add_lvalue_reference_t<const _Up>>>;
194
195// [concepts.arithmetic], arithmetic concepts
196template<class _Tp>
197concept integral = is_integral_v<_Tp>;
198
199template<class _Tp>
200concept signed_integral = integral<_Tp> && is_signed_v<_Tp>;
201
202template<class _Tp>
203concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>;
204
205template<class _Tp>
206concept floating_point = is_floating_point_v<_Tp>;
207
208// [concept.assignable]
209template<class _Lhs, class _Rhs>
210concept assignable_from =
211  is_lvalue_reference_v<_Lhs> &&
212  common_reference_with<__make_const_lvalue_ref<_Lhs>, __make_const_lvalue_ref<_Rhs>> &&
213  requires (_Lhs __lhs, _Rhs&& __rhs) {
214    { __lhs = _VSTD::forward<_Rhs>(__rhs) } -> same_as<_Lhs>;
215  };
216
217// [concept.destructible]
218
219template<class _Tp>
220concept destructible = is_nothrow_destructible_v<_Tp>;
221
222// [concept.constructible]
223template<class _Tp, class... _Args>
224concept constructible_from =
225    destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
226
227// [concept.default.init]
228
229template<class _Tp>
230concept __default_initializable = requires { ::new _Tp; };
231
232template<class _Tp>
233concept default_initializable = constructible_from<_Tp> &&
234    requires { _Tp{}; } && __default_initializable<_Tp>;
235
236// [concept.moveconstructible]
237template<class _Tp>
238concept move_constructible =
239  constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
240
241// [concept.copyconstructible]
242template<class _Tp>
243concept copy_constructible =
244  move_constructible<_Tp> &&
245  constructible_from<_Tp, _Tp&> && convertible_to<_Tp&, _Tp> &&
246  constructible_from<_Tp, const _Tp&> && convertible_to<const _Tp&, _Tp> &&
247  constructible_from<_Tp, const _Tp> && convertible_to<const _Tp, _Tp>;
248
249// Whether a type is a class type or enumeration type according to the Core wording.
250template<class _Tp>
251concept __class_or_enum = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;
252
253// [concept.swappable]
254namespace ranges::__swap {
255  // Deleted to inhibit ADL
256  template<class _Tp>
257  void swap(_Tp&, _Tp&) = delete;
258
259
260  // [1]
261  template<class _Tp, class _Up>
262  concept __unqualified_swappable_with =
263    (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&
264    requires(_Tp&& __t, _Up&& __u) {
265      swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
266    };
267
268  struct __fn;
269
270  template<class _Tp, class _Up, size_t _Size>
271  concept __swappable_arrays =
272    !__unqualified_swappable_with<_Tp(&)[_Size], _Up(&)[_Size]> &&
273    extent_v<_Tp> == extent_v<_Up> &&
274    requires(_Tp(& __t)[_Size], _Up(& __u)[_Size], const __fn& __swap) {
275      __swap(__t[0], __u[0]);
276    };
277
278  template<class _Tp>
279  concept __exchangeable =
280    !__unqualified_swappable_with<_Tp&, _Tp&> &&
281    move_constructible<_Tp> &&
282    assignable_from<_Tp&, _Tp>;
283
284  struct __fn {
285    // 2.1   `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...
286    // *The name `swap` is used here unqualified.
287    template<class _Tp, class _Up>
288    requires __unqualified_swappable_with<_Tp, _Up>
289    constexpr void operator()(_Tp&& __t, _Up&& __u) const
290    noexcept(noexcept(swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
291    {
292      swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
293    }
294
295    // 2.2   Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...
296    template<class _Tp, class _Up, size_t _Size>
297    requires __swappable_arrays<_Tp, _Up, _Size>
298    constexpr void operator()(_Tp(& __t)[_Size], _Up(& __u)[_Size]) const
299    noexcept(noexcept((*this)(*__t, *__u)))
300    {
301      // TODO(cjdb): replace with `ranges::swap_ranges`.
302      for (size_t __i = 0; __i < _Size; ++__i) {
303        (*this)(__t[__i], __u[__i]);
304      }
305    }
306
307    // 2.3   Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...
308    template<__exchangeable _Tp>
309    constexpr void operator()(_Tp& __x, _Tp& __y) const
310    noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>)
311    {
312      __y = _VSTD::exchange(__x, _VSTD::move(__y));
313    }
314  };
315} // namespace ranges::__swap
316
317namespace ranges::inline __cpo {
318  inline constexpr auto swap = __swap::__fn{};
319} // namespace ranges::__cpo
320
321template<class _Tp>
322concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
323
324template<class _Tp, class _Up>
325concept swappable_with =
326  common_reference_with<_Tp, _Up> &&
327  requires(_Tp&& __t, _Up&& __u) {
328    ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Tp>(__t));
329    ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Up>(__u));
330    ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
331    ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Tp>(__t));
332  };
333
334// [concept.booleantestable]
335template<class _Tp>
336concept __boolean_testable_impl = convertible_to<_Tp, bool>;
337
338template<class _Tp>
339concept __boolean_testable = __boolean_testable_impl<_Tp> && requires(_Tp&& __t) {
340  { !std::forward<_Tp>(__t) } -> __boolean_testable_impl;
341};
342
343// [concept.equalitycomparable]
344template<class _Tp, class _Up>
345concept __weakly_equality_comparable_with =
346  requires(__make_const_lvalue_ref<_Tp> __t, __make_const_lvalue_ref<_Up> __u) {
347    { __t == __u } -> __boolean_testable;
348    { __t != __u } -> __boolean_testable;
349    { __u == __t } -> __boolean_testable;
350    { __u != __t } -> __boolean_testable;
351  };
352
353template<class _Tp>
354concept equality_comparable = __weakly_equality_comparable_with<_Tp, _Tp>;
355
356template<class _Tp, class _Up>
357concept equality_comparable_with =
358  equality_comparable<_Tp> && equality_comparable<_Up> &&
359  common_reference_with<__make_const_lvalue_ref<_Tp>, __make_const_lvalue_ref<_Up>> &&
360  equality_comparable<
361    common_reference_t<
362      __make_const_lvalue_ref<_Tp>,
363      __make_const_lvalue_ref<_Up>>> &&
364  __weakly_equality_comparable_with<_Tp, _Up>;
365
366// [concept.totallyordered]
367
368template<class _Tp, class _Up>
369concept __partially_ordered_with =
370  requires(__make_const_lvalue_ref<_Tp> __t, __make_const_lvalue_ref<_Up> __u) {
371    { __t <  __u } -> __boolean_testable;
372    { __t >  __u } -> __boolean_testable;
373    { __t <= __u } -> __boolean_testable;
374    { __t >= __u } -> __boolean_testable;
375    { __u <  __t } -> __boolean_testable;
376    { __u >  __t } -> __boolean_testable;
377    { __u <= __t } -> __boolean_testable;
378    { __u >= __t } -> __boolean_testable;
379  };
380
381template<class _Tp>
382concept totally_ordered = equality_comparable<_Tp> && __partially_ordered_with<_Tp, _Tp>;
383
384template<class _Tp, class _Up>
385concept totally_ordered_with =
386  totally_ordered<_Tp> && totally_ordered<_Up> &&
387  equality_comparable_with<_Tp, _Up> &&
388  totally_ordered<
389    common_reference_t<
390      __make_const_lvalue_ref<_Tp>,
391      __make_const_lvalue_ref<_Up>>> &&
392  __partially_ordered_with<_Tp, _Up>;
393
394// [concepts.object]
395template<class _Tp>
396concept movable =
397  is_object_v<_Tp> &&
398  move_constructible<_Tp> &&
399  assignable_from<_Tp&, _Tp> &&
400  swappable<_Tp>;
401
402template<class _Tp>
403concept copyable =
404  copy_constructible<_Tp> &&
405  movable<_Tp> &&
406  assignable_from<_Tp&, _Tp&> &&
407  assignable_from<_Tp&, const _Tp&> &&
408  assignable_from<_Tp&, const _Tp>;
409
410template<class _Tp>
411concept semiregular = copyable<_Tp> && default_initializable<_Tp>;
412
413template<class _Tp>
414concept regular = semiregular<_Tp> && equality_comparable<_Tp>;
415
416// [concept.invocable]
417template<class _Fn, class... _Args>
418concept invocable = requires(_Fn&& __fn, _Args&&... __args) {
419  _VSTD::invoke(_VSTD::forward<_Fn>(__fn), _VSTD::forward<_Args>(__args)...); // not required to be equality preserving
420};
421
422// [concept.regular.invocable]
423template<class _Fn, class... _Args>
424concept regular_invocable = invocable<_Fn, _Args...>;
425
426// [concept.predicate]
427template<class _Fn, class... _Args>
428concept predicate =
429  regular_invocable<_Fn, _Args...> && __boolean_testable<invoke_result_t<_Fn, _Args...>>;
430
431// [concept.relation]
432template<class _Rp, class _Tp, class _Up>
433concept relation =
434  predicate<_Rp, _Tp, _Tp> && predicate<_Rp, _Up, _Up> &&
435  predicate<_Rp, _Tp, _Up> && predicate<_Rp, _Up, _Tp>;
436
437// [concept.equiv]
438template<class _Rp, class _Tp, class _Up>
439concept equivalence_relation = relation<_Rp, _Tp, _Up>;
440
441// [concept.strictweakorder]
442template<class _Rp, class _Tp, class _Up>
443concept strict_weak_order = relation<_Rp, _Tp, _Up>;
444
445#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
446
447_LIBCPP_END_NAMESPACE_STD
448
449_LIBCPP_POP_MACROS
450
451#endif // _LIBCPP_CONCEPTS
452