blob: 99b340f98cec1be8d5e8974b0e99f1b7020d12b3 [file] [log] [blame]
Eric Fiselierd59a0322020-04-08 18:00:13 -04001// -*- 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
Christopher Di Bella0c9ea042021-04-02 18:07:31 +000070 // [concept.default.init], concept default_initializable
Eric Fiselierd59a0322020-04-08 18:00:13 -040071 template<class T>
Christopher Di Bella0c9ea042021-04-02 18:07:31 +000072 concept default_initializable = see below;
Eric Fiselierd59a0322020-04-08 18:00:13 -040073
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
Eric Fiselierd59a0322020-04-08 18:00:13 -040082 // [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>
Christopher Di Bella55d7a822021-07-01 09:25:35 -0400133#include <__functional/invoke.h>
Christopher Di Bella8f0c5912021-03-21 18:48:24 +0000134#include <__functional_base>
Eric Fiselierd59a0322020-04-08 18:00:13 -0400135#include <type_traits>
Christopher Di Bella25c8ce62021-02-08 05:08:25 +0000136#include <utility>
Eric Fiselierd59a0322020-04-08 18:00:13 -0400137#include <version>
138
139#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
140#pragma GCC system_header
141#endif
142
143_LIBCPP_PUSH_MACROS
144#include <__undef_macros>
145
146_LIBCPP_BEGIN_NAMESPACE_STD
147
Christopher Di Bella88110022021-02-19 01:54:52 +0000148#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
Eric Fiselierd59a0322020-04-08 18:00:13 -0400149
150// [concept.same]
151
152template<class _Tp, class _Up>
Arthur O'Dwyer16407b82021-08-05 23:29:53 -0400153concept __same_as_impl = _IsSame<_Tp, _Up>::value;
Eric Fiselierd59a0322020-04-08 18:00:13 -0400154
155template<class _Tp, class _Up>
156concept same_as = __same_as_impl<_Tp, _Up> && __same_as_impl<_Up, _Tp>;
157
Christopher Di Bella2050d992021-02-04 03:41:44 +0000158// [concept.derived]
159template<class _Dp, class _Bp>
160concept derived_from =
161 is_base_of_v<_Bp, _Dp> &&
162 is_convertible_v<const volatile _Dp*, const volatile _Bp*>;
163
Christopher Di Bella16249f32021-02-04 03:19:26 +0000164// [concept.convertible]
165template<class _From, class _To>
166concept convertible_to =
167 is_convertible_v<_From, _To> &&
168 requires(add_rvalue_reference_t<_From> (&__f)()) {
169 static_cast<_To>(__f());
170 };
171
Christopher Di Bella550a5962021-03-02 13:48:06 -0800172// [concept.commonref]
173template<class _Tp, class _Up>
174concept common_reference_with =
175 same_as<common_reference_t<_Tp, _Up>, common_reference_t<_Up, _Tp>> &&
176 convertible_to<_Tp, common_reference_t<_Tp, _Up>> &&
177 convertible_to<_Up, common_reference_t<_Tp, _Up>>;
178
Christopher Di Bellaf138e262021-03-02 13:52:30 -0800179// [concept.common]
180template<class _Tp, class _Up>
181concept common_with =
182 same_as<common_type_t<_Tp, _Up>, common_type_t<_Up, _Tp>> &&
183 requires {
Arthur O'Dwyer3285c342021-05-10 13:04:16 -0400184 static_cast<common_type_t<_Tp, _Up>>(declval<_Tp>());
185 static_cast<common_type_t<_Tp, _Up>>(declval<_Up>());
Christopher Di Bellaf138e262021-03-02 13:52:30 -0800186 } &&
187 common_reference_with<
188 add_lvalue_reference_t<const _Tp>,
189 add_lvalue_reference_t<const _Up>> &&
190 common_reference_with<
191 add_lvalue_reference_t<common_type_t<_Tp, _Up>>,
192 common_reference_t<
193 add_lvalue_reference_t<const _Tp>,
194 add_lvalue_reference_t<const _Up>>>;
195
Christopher Di Bella71812802021-01-21 02:33:04 +0000196// [concepts.arithmetic], arithmetic concepts
197template<class _Tp>
198concept integral = is_integral_v<_Tp>;
199
200template<class _Tp>
201concept signed_integral = integral<_Tp> && is_signed_v<_Tp>;
202
203template<class _Tp>
204concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>;
205
206template<class _Tp>
207concept floating_point = is_floating_point_v<_Tp>;
208
Christopher Di Bella6054b622021-03-03 22:27:21 -0800209// [concept.assignable]
210template<class _Lhs, class _Rhs>
211concept assignable_from =
212 is_lvalue_reference_v<_Lhs> &&
Christopher Di Bellacee8c842021-03-31 21:42:37 +0000213 common_reference_with<__make_const_lvalue_ref<_Lhs>, __make_const_lvalue_ref<_Rhs>> &&
Christopher Di Bella6054b622021-03-03 22:27:21 -0800214 requires (_Lhs __lhs, _Rhs&& __rhs) {
215 { __lhs = _VSTD::forward<_Rhs>(__rhs) } -> same_as<_Lhs>;
216 };
217
Mark de Wevere9557352020-11-24 16:12:34 +0100218// [concept.destructible]
219
220template<class _Tp>
Mark de Wever9d7aa7a2021-05-09 18:22:52 +0200221concept destructible = is_nothrow_destructible_v<_Tp>;
Mark de Wevere9557352020-11-24 16:12:34 +0100222
Mark de Wever0582b602020-12-17 06:53:00 +0100223// [concept.constructible]
224template<class _Tp, class... _Args>
225concept constructible_from =
Mark de Wever9d7aa7a2021-05-09 18:22:52 +0200226 destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
Mark de Wever0582b602020-12-17 06:53:00 +0100227
Mark de Wever80379212020-12-17 07:48:26 +0100228// [concept.default.init]
229
230template<class _Tp>
231concept __default_initializable = requires { ::new _Tp; };
232
233template<class _Tp>
234concept default_initializable = constructible_from<_Tp> &&
235 requires { _Tp{}; } && __default_initializable<_Tp>;
236
Christopher Di Bellae60e7db2021-02-08 02:00:42 +0000237// [concept.moveconstructible]
238template<class _Tp>
239concept move_constructible =
240 constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
241
Christopher Di Bellaba6c14f2021-03-04 22:07:45 -0800242// [concept.copyconstructible]
243template<class _Tp>
244concept copy_constructible =
245 move_constructible<_Tp> &&
246 constructible_from<_Tp, _Tp&> && convertible_to<_Tp&, _Tp> &&
247 constructible_from<_Tp, const _Tp&> && convertible_to<const _Tp&, _Tp> &&
248 constructible_from<_Tp, const _Tp> && convertible_to<const _Tp, _Tp>;
249
Louis Dionneca989a52021-04-20 14:40:43 -0400250// Whether a type is a class type or enumeration type according to the Core wording.
251template<class _Tp>
252concept __class_or_enum = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;
253
Christopher Di Bellacfaf7cf2021-03-03 22:47:06 -0800254// [concept.swappable]
255namespace ranges::__swap {
256 // Deleted to inhibit ADL
257 template<class _Tp>
258 void swap(_Tp&, _Tp&) = delete;
259
Christopher Di Bellacfaf7cf2021-03-03 22:47:06 -0800260
261 // [1]
262 template<class _Tp, class _Up>
263 concept __unqualified_swappable_with =
264 (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&
265 requires(_Tp&& __t, _Up&& __u) {
266 swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
267 };
268
269 struct __fn;
270
271 template<class _Tp, class _Up, size_t _Size>
272 concept __swappable_arrays =
273 !__unqualified_swappable_with<_Tp(&)[_Size], _Up(&)[_Size]> &&
274 extent_v<_Tp> == extent_v<_Up> &&
275 requires(_Tp(& __t)[_Size], _Up(& __u)[_Size], const __fn& __swap) {
276 __swap(__t[0], __u[0]);
277 };
278
279 template<class _Tp>
280 concept __exchangeable =
281 !__unqualified_swappable_with<_Tp&, _Tp&> &&
282 move_constructible<_Tp> &&
283 assignable_from<_Tp&, _Tp>;
284
285 struct __fn {
286 // 2.1 `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...
287 // *The name `swap` is used here unqualified.
288 template<class _Tp, class _Up>
289 requires __unqualified_swappable_with<_Tp, _Up>
290 constexpr void operator()(_Tp&& __t, _Up&& __u) const
291 noexcept(noexcept(swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
292 {
293 swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
294 }
295
296 // 2.2 Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...
297 template<class _Tp, class _Up, size_t _Size>
298 requires __swappable_arrays<_Tp, _Up, _Size>
299 constexpr void operator()(_Tp(& __t)[_Size], _Up(& __u)[_Size]) const
300 noexcept(noexcept((*this)(*__t, *__u)))
301 {
302 // TODO(cjdb): replace with `ranges::swap_ranges`.
303 for (size_t __i = 0; __i < _Size; ++__i) {
304 (*this)(__t[__i], __u[__i]);
305 }
306 }
307
308 // 2.3 Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...
309 template<__exchangeable _Tp>
310 constexpr void operator()(_Tp& __x, _Tp& __y) const
311 noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>)
312 {
313 __y = _VSTD::exchange(__x, _VSTD::move(__y));
314 }
315 };
316} // namespace ranges::__swap
317
318namespace ranges::inline __cpo {
319 inline constexpr auto swap = __swap::__fn{};
320} // namespace ranges::__cpo
321
322template<class _Tp>
323concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
324
325template<class _Tp, class _Up>
326concept swappable_with =
Christopher Di Bella121f6142021-04-02 21:33:39 +0000327 common_reference_with<_Tp, _Up> &&
Christopher Di Bellacfaf7cf2021-03-03 22:47:06 -0800328 requires(_Tp&& __t, _Up&& __u) {
329 ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Tp>(__t));
330 ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Up>(__u));
331 ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
332 ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Tp>(__t));
333 };
334
Christopher Di Bellaa9dfeb02021-03-04 17:18:23 -0800335// [concept.booleantestable]
336template<class _Tp>
337concept __boolean_testable_impl = convertible_to<_Tp, bool>;
338
339template<class _Tp>
340concept __boolean_testable = __boolean_testable_impl<_Tp> && requires(_Tp&& __t) {
341 { !std::forward<_Tp>(__t) } -> __boolean_testable_impl;
342};
343
344// [concept.equalitycomparable]
345template<class _Tp, class _Up>
346concept __weakly_equality_comparable_with =
Christopher Di Bellacee8c842021-03-31 21:42:37 +0000347 requires(__make_const_lvalue_ref<_Tp> __t, __make_const_lvalue_ref<_Up> __u) {
Christopher Di Bellaa9dfeb02021-03-04 17:18:23 -0800348 { __t == __u } -> __boolean_testable;
349 { __t != __u } -> __boolean_testable;
350 { __u == __t } -> __boolean_testable;
351 { __u != __t } -> __boolean_testable;
352 };
353
354template<class _Tp>
355concept equality_comparable = __weakly_equality_comparable_with<_Tp, _Tp>;
356
357template<class _Tp, class _Up>
358concept equality_comparable_with =
359 equality_comparable<_Tp> && equality_comparable<_Up> &&
Christopher Di Bellacee8c842021-03-31 21:42:37 +0000360 common_reference_with<__make_const_lvalue_ref<_Tp>, __make_const_lvalue_ref<_Up>> &&
Christopher Di Bellaa9dfeb02021-03-04 17:18:23 -0800361 equality_comparable<
362 common_reference_t<
Christopher Di Bellacee8c842021-03-31 21:42:37 +0000363 __make_const_lvalue_ref<_Tp>,
364 __make_const_lvalue_ref<_Up>>> &&
Christopher Di Bellaa9dfeb02021-03-04 17:18:23 -0800365 __weakly_equality_comparable_with<_Tp, _Up>;
366
Christopher Di Bellacee8c842021-03-31 21:42:37 +0000367// [concept.totallyordered]
368
369template<class _Tp, class _Up>
370concept __partially_ordered_with =
371 requires(__make_const_lvalue_ref<_Tp> __t, __make_const_lvalue_ref<_Up> __u) {
372 { __t < __u } -> __boolean_testable;
373 { __t > __u } -> __boolean_testable;
374 { __t <= __u } -> __boolean_testable;
375 { __t >= __u } -> __boolean_testable;
376 { __u < __t } -> __boolean_testable;
377 { __u > __t } -> __boolean_testable;
378 { __u <= __t } -> __boolean_testable;
379 { __u >= __t } -> __boolean_testable;
380 };
381
382template<class _Tp>
383concept totally_ordered = equality_comparable<_Tp> && __partially_ordered_with<_Tp, _Tp>;
384
385template<class _Tp, class _Up>
386concept totally_ordered_with =
387 totally_ordered<_Tp> && totally_ordered<_Up> &&
388 equality_comparable_with<_Tp, _Up> &&
389 totally_ordered<
390 common_reference_t<
391 __make_const_lvalue_ref<_Tp>,
392 __make_const_lvalue_ref<_Up>>> &&
393 __partially_ordered_with<_Tp, _Up>;
394
Christopher Di Bella95683be2021-03-11 18:54:57 -0800395// [concepts.object]
396template<class _Tp>
397concept movable =
398 is_object_v<_Tp> &&
399 move_constructible<_Tp> &&
400 assignable_from<_Tp&, _Tp> &&
401 swappable<_Tp>;
402
Christopher Di Bella0c039642021-03-11 19:30:55 -0800403template<class _Tp>
404concept copyable =
405 copy_constructible<_Tp> &&
406 movable<_Tp> &&
407 assignable_from<_Tp&, _Tp&> &&
408 assignable_from<_Tp&, const _Tp&> &&
409 assignable_from<_Tp&, const _Tp>;
410
Christopher Di Bella357ae112021-03-11 19:49:27 -0800411template<class _Tp>
412concept semiregular = copyable<_Tp> && default_initializable<_Tp>;
413
Christopher Di Bella75c652d2021-03-11 23:46:37 -0800414template<class _Tp>
415concept regular = semiregular<_Tp> && equality_comparable<_Tp>;
416
Christopher Di Bella25c8ce62021-02-08 05:08:25 +0000417// [concept.invocable]
418template<class _Fn, class... _Args>
419concept invocable = requires(_Fn&& __fn, _Args&&... __args) {
420 _VSTD::invoke(_VSTD::forward<_Fn>(__fn), _VSTD::forward<_Args>(__args)...); // not required to be equality preserving
421};
422
423// [concept.regular.invocable]
424template<class _Fn, class... _Args>
425concept regular_invocable = invocable<_Fn, _Args...>;
426
Christopher Di Bellae31461a2021-03-31 05:28:25 +0000427// [concept.predicate]
428template<class _Fn, class... _Args>
429concept predicate =
430 regular_invocable<_Fn, _Args...> && __boolean_testable<invoke_result_t<_Fn, _Args...>>;
431
432// [concept.relation]
433template<class _Rp, class _Tp, class _Up>
434concept relation =
435 predicate<_Rp, _Tp, _Tp> && predicate<_Rp, _Up, _Up> &&
436 predicate<_Rp, _Tp, _Up> && predicate<_Rp, _Up, _Tp>;
437
438// [concept.equiv]
439template<class _Rp, class _Tp, class _Up>
440concept equivalence_relation = relation<_Rp, _Tp, _Up>;
441
442// [concept.strictweakorder]
443template<class _Rp, class _Tp, class _Up>
444concept strict_weak_order = relation<_Rp, _Tp, _Up>;
zoecarver61ae1dc2021-04-19 14:28:27 -0400445
zoecarver55fe1b92021-06-01 14:54:29 -0700446template<class _Tp, class _Up>
447concept __different_from = !same_as<remove_cvref_t<_Tp>, remove_cvref_t<_Up>>;
448
Louis Dionneca989a52021-04-20 14:40:43 -0400449#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
Eric Fiselierd59a0322020-04-08 18:00:13 -0400450
451_LIBCPP_END_NAMESPACE_STD
452
453_LIBCPP_POP_MACROS
454
455#endif // _LIBCPP_CONCEPTS