Implement <concepts> header for C++20
[official-gcc.git] / libstdc++-v3 / include / std / concepts
blob6b1150a32d21f9c13fb9bd52b8e6e57396d95a7d
1 // <concepts> -*- C++ -*-
3 // Copyright (C) 2019 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.
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.
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.
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/>.
25 /** @file include/concepts
26  *  This is __a Standard C++ Library header.
27  *  @ingroup concepts
28  */
30 #ifndef _GLIBCXX_CONCEPTS
31 #define _GLIBCXX_CONCEPTS 1
33 #if __cplusplus > 201703L && __cpp_concepts
35 #pragma GCC system_header
37 /**
38  * @defgroup concepts Concepts
39  * @ingroup utilities
40  *
41  * Concepts for checking type requirements.
42  */
44 #include <type_traits>
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 #define __cpp_lib_concepts 201806L
52   // [concepts.lang], language-related concepts
54   namespace __detail
55   {
56     template<typename _Tp, typename _Up>
57       concept __same_as = __is_same_as(_Tp, _Up);
58   } // namespace __detail
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>;
65   /// [concept.derived], concept derived_from
66   template<typename _Derived, typename _Base>
67     concept derived_from = __is_base_of(_Base, _Derived)
68       && is_convertible_v<const volatile _Derived*, const volatile _Base*>;
70   /// [concept.convertible], concept convertible_to
71   template<typename _From, typename _To>
72     concept convertible_to = is_convertible_v<_From, _To>
73       && requires(add_rvalue_reference_t<_From> (&__f)()) {
74           static_cast<_To>(__f());
75       };
77   /// [concept.commonref], concept common_reference_with
78   template<typename _Tp, typename _Up>
79     concept common_reference_with
80       = same_as<common_reference_t<_Tp, _Up>, common_reference_t<_Up, _Tp>>
81       && convertible_to<_Tp, common_reference_t<_Tp, _Up>>
82       && convertible_to<_Up, common_reference_t<_Tp, _Up>>;
84   /// [concept.common], concept common_with
85   template<typename _Tp, typename _Up>
86     concept common_with
87       = same_as<common_type_t<_Tp, _Up>, common_type_t<_Up, _Tp>>
88       && requires {
89         static_cast<common_type_t<_Tp, _Up>>(std::declval<_Tp>());
90         static_cast<common_type_t<_Tp, _Up>>(std::declval<_Up>());
91       }
92       && common_reference_with<add_lvalue_reference_t<const _Tp>,
93                                add_lvalue_reference_t<const _Up>>
94       && common_reference_with<add_lvalue_reference_t<common_type_t<_Tp, _Up>>,
95                                common_reference_t<
96                                  add_lvalue_reference_t<const _Tp>,
97                                  add_lvalue_reference_t<const _Up>>>;
99   // [concepts.arithmetic], arithmetic concepts
101   template<typename _Tp>
102     concept integral = is_integral_v<_Tp>;
104   template<typename _Tp>
105     concept signed_integral = integral<_Tp> && is_signed_v<_Tp>;
107   template<typename _Tp>
108     concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>;
110   template<typename _Tp>
111     concept floating_point = is_floating_point_v<_Tp>;
113   namespace __detail
114   {
115     template<typename _Tp>
116       using __cref = const remove_reference_t<_Tp>&;
117   } // namespace __detail
119   /// [concept.assignable], concept assignable_from
120   template<typename _Lhs, typename _Rhs>
121     concept assignable_from
122       = is_lvalue_reference_v<_Lhs>
123       && common_reference_with<__detail::__cref<_Lhs>, __detail::__cref<_Rhs>>
124       && requires(_Lhs __lhs, _Rhs&& __rhs) {
125         { __lhs = static_cast<_Rhs&&>(__rhs) } -> same_as<_Lhs>;
126       };
128   /// [concept.destructible], concept destructible
129   template<typename _Tp>
130     concept destructible = is_nothrow_destructible_v<_Tp>;
132   /// [concept.constructible], concept constructible_from
133   template<typename _Tp, typename... _Args>
134     concept constructible_from
135       = destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
137   /// [concept.defaultconstructible], concept default_constructible
138   template<typename _Tp>
139     concept default_constructible = constructible_from<_Tp>;
141   /// [concept.moveconstructible], concept move_constructible
142   template<typename _Tp>
143     concept move_constructible
144     = constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
146   /// [concept.copyconstructible], concept copy_constructible
147   template<typename _Tp>
148     concept copy_constructible
149       = move_constructible<_Tp>
150       && constructible_from<_Tp, _Tp&> && convertible_to<_Tp&, _Tp>
151       && constructible_from<_Tp, const _Tp&> && convertible_to<const _Tp&, _Tp>
152       && constructible_from<_Tp, const _Tp> && convertible_to<const _Tp, _Tp>;
154   // [concept.swappable], concept swappable
156   namespace ranges
157   {
158     namespace __cust_swap
159     {
160       template<typename _Tp> void swap(_Tp&, _Tp&) = delete;
162       template<typename _Tp>
163         concept __class_or_enum
164           = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;
166       template<typename _Tp, typename _Up>
167         concept __adl_swap
168           = (__class_or_enum<remove_cvref_t<_Tp>>
169             || __class_or_enum<remove_cvref_t<_Up>>)
170           && requires(_Tp&& __t, _Up&& __u) {
171             swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
172           };
174       struct _Swap
175       {
176         template<typename _Tp, typename _Up>
177           requires __adl_swap<_Tp, _Up>
178           constexpr void operator()(_Tp&& __t, _Up&& __u) const
179           noexcept(noexcept(swap(std::declval<_Tp>(), std::declval<_Up>())))
180           { swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); }
182         template<typename _Tp, typename _Up, size_t _Num>
183           requires requires(const _Swap& __swap, _Tp& __e1, _Up& __e2) {
184             __swap(__e1, __e2);
185           }
186           constexpr void
187           operator()(_Tp (&__e1)[_Num], _Up (&__e2)[_Num]) const
188           noexcept(noexcept(std::declval<const _Swap&>()(*__e1, *__e2)))
189           {
190             for (size_t __n = 0; __n < _Num; ++__n)
191               (*this)(__e1[__n], __e2[__n]);
192           }
194         template<typename _Tp>
195           requires (!__adl_swap<_Tp&, _Tp&>
196             && move_constructible<_Tp> && assignable_from<_Tp&, _Tp>)
197           constexpr void
198           operator()(_Tp& __e1, _Tp& __e2) const
199           noexcept(is_nothrow_move_constructible_v<_Tp>
200                    && is_nothrow_move_assignable_v<_Tp>)
201           {
202             _Tp __tmp = static_cast<_Tp&&>(__e1);
203             __e1 = static_cast<_Tp&&>(__e2);
204             __e2 = static_cast<_Tp&&>(__tmp);
205           }
206       };
207     } // namespace __cust_swap
209     inline namespace __cust
210     {
211       inline constexpr __cust_swap::_Swap swap{};
212     } // inline namespace __cust
213   } // namespace ranges
215   template<typename _Tp>
216     concept swappable
217       = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
219   template<typename _Tp, typename _Up>
220     concept swappable_with = common_reference_with<_Tp, _Up>
221       && requires(_Tp&& __t, _Up&& __u) {
222         ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Tp&&>(__t));
223         ranges::swap(static_cast<_Up&&>(__u), static_cast<_Up&&>(__u));
224         ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
225         ranges::swap(static_cast<_Up&&>(__u), static_cast<_Tp&&>(__t));
226       };
228   // [concepts.object], Object concepts
230   template<typename _Tp>
231     concept movable = is_object_v<_Tp> && move_constructible<_Tp>
232       && assignable_from<_Tp&, _Tp> && swappable<_Tp>;
234   template<typename _Tp>
235     concept copyable = copy_constructible<_Tp> && movable<_Tp>
236       && assignable_from<_Tp&, const _Tp&>;
238   template<typename _Tp>
239     concept semiregular = copyable<_Tp> && default_constructible<_Tp>;
241   // [concepts.compare], comparison concepts
243   /// [concept.boolean], concept boolean
244   template<typename _Bp>
245     concept boolean
246       = movable<remove_cvref_t<_Bp>>
247       && requires(__detail::__cref<_Bp> __b1, __detail::__cref<_Bp> __b2,
248                   const bool __a) {
249         { __b1 } -> convertible_to<bool>;
250         { !__b1 } -> convertible_to<bool>;
251         { __b1 && __b2 } -> same_as<bool>;
252         { __b1 && __a  } -> same_as<bool>;
253         { __a  && __b2 } -> same_as<bool>;
254         { __b1 || __b2 } -> same_as<bool>;
255         { __b1 || __a  } -> same_as<bool>;
256         { __a  || __b2 } -> same_as<bool>;
257         { __b1 == __b2 } -> convertible_to<bool>;
258         { __b1 == __a  } -> convertible_to<bool>;
259         { __a  == __b2 } -> convertible_to<bool>;
260         { __b1 != __b2 } -> convertible_to<bool>;
261         { __b1 != __a  } -> convertible_to<bool>;
262         { __a  != __b2 } -> convertible_to<bool>;
263       };
265   // [concept.equalitycomparable], concept equality_comparable
267   namespace __detail
268   {
269     template<typename _Tp, typename _Up>
270       concept __weakly_eq_cmp_with
271         = requires(__detail::__cref<_Tp> __t, __detail::__cref<_Up> __u) {
272           { __t == __u } -> boolean;
273           { __t != __u } -> boolean;
274           { __u == __t } -> boolean;
275           { __u != __t } -> boolean;
276         };
277   } // namespace __detail
279   template<typename _Tp>
280     concept equality_comparable = __detail::__weakly_eq_cmp_with<_Tp, _Tp>;
282   template<typename _Tp, typename _Up>
283     concept equality_comparable_with
284       = equality_comparable<_Tp> && equality_comparable<_Up>
285       && common_reference_with<__detail::__cref<_Tp>, __detail::__cref<_Up>>
286       && equality_comparable<common_reference_t<__detail::__cref<_Tp>,
287                                                 __detail::__cref<_Up>>>
288       && __detail::__weakly_eq_cmp_with<_Tp, _Up>;
290   // [concept.totallyordered], concept totally_ordered
291   template<typename _Tp>
292     concept totally_ordered
293       = equality_comparable<_Tp>
294       && requires(__detail::__cref<_Tp> __a, __detail::__cref<_Tp> __b) {
295         { __a <  __b } -> boolean;
296         { __a >  __b } -> boolean;
297         { __a <= __b } -> boolean;
298         { __a >= __b } -> boolean;
299       };
301   template<typename _Tp, typename _Up>
302     concept totally_ordered_with
303       = totally_ordered<_Tp> && totally_ordered<_Up>
304       && common_reference_with<__detail::__cref<_Tp>, __detail::__cref<_Up>>
305       && totally_ordered<common_reference_t<__detail::__cref<_Tp>,
306                                             __detail::__cref<_Up>>>
307       && equality_comparable_with<_Tp, _Up>
308       && requires(__detail::__cref<_Tp> __t, __detail::__cref<_Up> __u) {
309         { __t <  __u } -> boolean;
310         { __t >  __u } -> boolean;
311         { __t <= __u } -> boolean;
312         { __t >= __u } -> boolean;
313         { __u <  __t } -> boolean;
314         { __u >  __t } -> boolean;
315         { __u <= __t } -> boolean;
316         { __u >= __t } -> boolean;
317       };
319   template<typename _Tp>
320     concept regular = semiregular<_Tp> && equality_comparable<_Tp>;
322   // [concepts.callable], callable concepts
324   // [concept.invocable], concept invocable
325   template<typename _Fn, typename... _Args>
326     concept invocable = is_invocable_v<_Fn, _Args...>;
328   // [concept.regularinvocable], concept regular_invocable
329   template<typename _Fn, typename... _Args>
330     concept regular_invocable = invocable<_Fn, _Args...>;
332   // [concept.predicate], concept predicate
333   template<typename _Fn, typename... _Args>
334     concept predicate = regular_invocable<_Fn, _Args...>
335       && boolean<invoke_result_t<_Fn, _Args...>>;
337   // [concept.relation], concept relation
338   template<typename _Rel, typename _Tp, typename _Up>
339     concept relation
340       = predicate<_Rel, _Tp, _Tp> && predicate<_Rel, _Up, _Up>
341       && predicate<_Rel, _Tp, _Up> && predicate<_Rel, _Up, _Tp>;
343   // [concept.strictweakorder], concept strict_weak_order
344   template<typename _Rel, typename _Tp, typename _Up>
345     concept strict_weak_order = relation<_Rel, _Tp, _Up>;
347 _GLIBCXX_END_NAMESPACE_VERSION
348 } // namespace
349 #endif // C++2a
351 #endif /* _GLIBCXX_CONCEPTS */