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>>(_VSTD::declval<_Tp>());
184    static_cast<common_type_t<_Tp, _Up>>(_VSTD::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 = _VSTD::is_nothrow_destructible_v<_Tp>;
221
222// [concept.constructible]
223template<class _Tp, class... _Args>
224concept constructible_from =
225    destructible<_Tp> && _VSTD::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// [concept.swappable]
250namespace ranges::__swap {
251  // Deleted to inhibit ADL
252  template<class _Tp>
253  void swap(_Tp&, _Tp&) = delete;
254
255  template<class _Tp>
256  concept __class_or_enum = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;
257
258  // [1]
259  template<class _Tp, class _Up>
260  concept __unqualified_swappable_with =
261    (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&
262    requires(_Tp&& __t, _Up&& __u) {
263      swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
264    };
265
266  struct __fn;
267
268  template<class _Tp, class _Up, size_t _Size>
269  concept __swappable_arrays =
270    !__unqualified_swappable_with<_Tp(&)[_Size], _Up(&)[_Size]> &&
271    extent_v<_Tp> == extent_v<_Up> &&
272    requires(_Tp(& __t)[_Size], _Up(& __u)[_Size], const __fn& __swap) {
273      __swap(__t[0], __u[0]);
274    };
275
276  template<class _Tp>
277  concept __exchangeable =
278    !__unqualified_swappable_with<_Tp&, _Tp&> &&
279    move_constructible<_Tp> &&
280    assignable_from<_Tp&, _Tp>;
281
282  struct __fn {
283    // 2.1   `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...
284    // *The name `swap` is used here unqualified.
285    template<class _Tp, class _Up>
286    requires __unqualified_swappable_with<_Tp, _Up>
287    constexpr void operator()(_Tp&& __t, _Up&& __u) const
288    noexcept(noexcept(swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
289    {
290      swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
291    }
292
293    // 2.2   Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...
294    template<class _Tp, class _Up, size_t _Size>
295    requires __swappable_arrays<_Tp, _Up, _Size>
296    constexpr void operator()(_Tp(& __t)[_Size], _Up(& __u)[_Size]) const
297    noexcept(noexcept((*this)(*__t, *__u)))
298    {
299      // TODO(cjdb): replace with `ranges::swap_ranges`.
300      for (size_t __i = 0; __i < _Size; ++__i) {
301        (*this)(__t[__i], __u[__i]);
302      }
303    }
304
305    // 2.3   Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...
306    template<__exchangeable _Tp>
307    constexpr void operator()(_Tp& __x, _Tp& __y) const
308    noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>)
309    {
310      __y = _VSTD::exchange(__x, _VSTD::move(__y));
311    }
312  };
313} // namespace ranges::__swap
314
315namespace ranges::inline __cpo {
316  inline constexpr auto swap = __swap::__fn{};
317} // namespace ranges::__cpo
318
319template<class _Tp>
320concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
321
322template<class _Tp, class _Up>
323concept swappable_with =
324  common_reference_with<_Tp, _Up> &&
325  requires(_Tp&& __t, _Up&& __u) {
326    ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Tp>(__t));
327    ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Up>(__u));
328    ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
329    ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Tp>(__t));
330  };
331
332// [concept.booleantestable]
333template<class _Tp>
334concept __boolean_testable_impl = convertible_to<_Tp, bool>;
335
336template<class _Tp>
337concept __boolean_testable = __boolean_testable_impl<_Tp> && requires(_Tp&& __t) {
338  { !std::forward<_Tp>(__t) } -> __boolean_testable_impl;
339};
340
341// [concept.equalitycomparable]
342template<class _Tp, class _Up>
343concept __weakly_equality_comparable_with =
344  requires(__make_const_lvalue_ref<_Tp> __t, __make_const_lvalue_ref<_Up> __u) {
345    { __t == __u } -> __boolean_testable;
346    { __t != __u } -> __boolean_testable;
347    { __u == __t } -> __boolean_testable;
348    { __u != __t } -> __boolean_testable;
349  };
350
351template<class _Tp>
352concept equality_comparable = __weakly_equality_comparable_with<_Tp, _Tp>;
353
354template<class _Tp, class _Up>
355concept equality_comparable_with =
356  equality_comparable<_Tp> && equality_comparable<_Up> &&
357  common_reference_with<__make_const_lvalue_ref<_Tp>, __make_const_lvalue_ref<_Up>> &&
358  equality_comparable<
359    common_reference_t<
360      __make_const_lvalue_ref<_Tp>,
361      __make_const_lvalue_ref<_Up>>> &&
362  __weakly_equality_comparable_with<_Tp, _Up>;
363
364// [concept.totallyordered]
365
366template<class _Tp, class _Up>
367concept __partially_ordered_with =
368  requires(__make_const_lvalue_ref<_Tp> __t, __make_const_lvalue_ref<_Up> __u) {
369    { __t <  __u } -> __boolean_testable;
370    { __t >  __u } -> __boolean_testable;
371    { __t <= __u } -> __boolean_testable;
372    { __t >= __u } -> __boolean_testable;
373    { __u <  __t } -> __boolean_testable;
374    { __u >  __t } -> __boolean_testable;
375    { __u <= __t } -> __boolean_testable;
376    { __u >= __t } -> __boolean_testable;
377  };
378
379template<class _Tp>
380concept totally_ordered = equality_comparable<_Tp> && __partially_ordered_with<_Tp, _Tp>;
381
382template<class _Tp, class _Up>
383concept totally_ordered_with =
384  totally_ordered<_Tp> && totally_ordered<_Up> &&
385  equality_comparable_with<_Tp, _Up> &&
386  totally_ordered<
387    common_reference_t<
388      __make_const_lvalue_ref<_Tp>,
389      __make_const_lvalue_ref<_Up>>> &&
390  __partially_ordered_with<_Tp, _Up>;
391
392// [concepts.object]
393template<class _Tp>
394concept movable =
395  is_object_v<_Tp> &&
396  move_constructible<_Tp> &&
397  assignable_from<_Tp&, _Tp> &&
398  swappable<_Tp>;
399
400template<class _Tp>
401concept copyable =
402  copy_constructible<_Tp> &&
403  movable<_Tp> &&
404  assignable_from<_Tp&, _Tp&> &&
405  assignable_from<_Tp&, const _Tp&> &&
406  assignable_from<_Tp&, const _Tp>;
407
408template<class _Tp>
409concept semiregular = copyable<_Tp> && default_initializable<_Tp>;
410
411template<class _Tp>
412concept regular = semiregular<_Tp> && equality_comparable<_Tp>;
413
414// [concept.invocable]
415template<class _Fn, class... _Args>
416concept invocable = requires(_Fn&& __fn, _Args&&... __args) {
417  _VSTD::invoke(_VSTD::forward<_Fn>(__fn), _VSTD::forward<_Args>(__args)...); // not required to be equality preserving
418};
419
420// [concept.regular.invocable]
421template<class _Fn, class... _Args>
422concept regular_invocable = invocable<_Fn, _Args...>;
423
424// [concept.predicate]
425template<class _Fn, class... _Args>
426concept predicate =
427  regular_invocable<_Fn, _Args...> && __boolean_testable<invoke_result_t<_Fn, _Args...>>;
428
429// [concept.relation]
430template<class _Rp, class _Tp, class _Up>
431concept relation =
432  predicate<_Rp, _Tp, _Tp> && predicate<_Rp, _Up, _Up> &&
433  predicate<_Rp, _Tp, _Up> && predicate<_Rp, _Up, _Tp>;
434
435// [concept.equiv]
436template<class _Rp, class _Tp, class _Up>
437concept equivalence_relation = relation<_Rp, _Tp, _Up>;
438
439// [concept.strictweakorder]
440template<class _Rp, class _Tp, class _Up>
441concept strict_weak_order = relation<_Rp, _Tp, _Up>;
442#endif //_LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
443
444_LIBCPP_END_NAMESPACE_STD
445
446_LIBCPP_POP_MACROS
447
448#endif // _LIBCPP_CONCEPTS
449