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