1 // <concepts> -*- C++ -*-
3 // Copyright (C) 2019 Free Software Foundation, Inc.
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)
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.
30 #ifndef _GLIBCXX_CONCEPTS
31 #define _GLIBCXX_CONCEPTS 1
33 #if __cplusplus > 201703L && __cpp_concepts
35 #pragma GCC system_header
38 * @defgroup concepts Concepts
41 * Concepts for checking type requirements.
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
56 template<typename _Tp, typename _Up>
57 concept __same_as = std::is_same_v<_Tp, _Up>;
58 } // namespace __detail
60 /// [concept.same], concept same_as
61 template<typename _Tp, typename _Up>
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());
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>
87 = same_as<common_type_t<_Tp, _Up>, common_type_t<_Up, _Tp>>
89 static_cast<common_type_t<_Tp, _Up>>(std::declval<_Tp>());
90 static_cast<common_type_t<_Tp, _Up>>(std::declval<_Up>());
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>>,
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>;
115 template<typename _Tp>
116 using __cref = const remove_reference_t<_Tp>&;
118 template<typename _Tp>
119 concept __class_or_enum
120 = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;
121 } // namespace __detail
123 /// [concept.assignable], concept assignable_from
124 template<typename _Lhs, typename _Rhs>
125 concept assignable_from
126 = is_lvalue_reference_v<_Lhs>
127 && common_reference_with<__detail::__cref<_Lhs>, __detail::__cref<_Rhs>>
128 && requires(_Lhs __lhs, _Rhs&& __rhs) {
129 { __lhs = static_cast<_Rhs&&>(__rhs) } -> same_as<_Lhs>;
132 /// [concept.destructible], concept destructible
133 template<typename _Tp>
134 concept destructible = is_nothrow_destructible_v<_Tp>;
136 /// [concept.constructible], concept constructible_from
137 template<typename _Tp, typename... _Args>
138 concept constructible_from
139 = destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
141 /// [concept.defaultconstructible], concept default_constructible
142 template<typename _Tp>
143 concept default_constructible = constructible_from<_Tp>;
145 /// [concept.moveconstructible], concept move_constructible
146 template<typename _Tp>
147 concept move_constructible
148 = constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
150 /// [concept.copyconstructible], concept copy_constructible
151 template<typename _Tp>
152 concept copy_constructible
153 = move_constructible<_Tp>
154 && constructible_from<_Tp, _Tp&> && convertible_to<_Tp&, _Tp>
155 && constructible_from<_Tp, const _Tp&> && convertible_to<const _Tp&, _Tp>
156 && constructible_from<_Tp, const _Tp> && convertible_to<const _Tp, _Tp>;
158 // [concept.swappable], concept swappable
162 namespace __cust_swap
164 template<typename _Tp> void swap(_Tp&, _Tp&) = delete;
166 template<typename _Tp, typename _Up>
168 = (__detail::__class_or_enum<remove_reference_t<_Tp>>
169 || __detail::__class_or_enum<remove_reference_t<_Up>>)
170 && requires(_Tp&& __t, _Up&& __u) {
171 swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
177 template<typename _Tp, typename _Up>
178 static constexpr bool
181 if constexpr (__adl_swap<_Tp, _Up>)
182 return noexcept(swap(std::declval<_Tp>(), std::declval<_Up>()));
184 return is_nothrow_move_constructible_v<remove_reference_t<_Tp>>
185 && is_nothrow_move_assignable_v<remove_reference_t<_Tp>>;
189 template<typename _Tp, typename _Up>
190 requires __adl_swap<_Tp, _Up>
191 || (same_as<_Tp, _Up> && is_lvalue_reference_v<_Tp>
192 && move_constructible<remove_reference_t<_Tp>>
193 && assignable_from<_Tp, remove_reference_t<_Tp>>)
195 operator()(_Tp&& __t, _Up&& __u) const
196 noexcept(_S_noexcept<_Tp, _Up>())
198 if constexpr (__adl_swap<_Tp, _Up>)
199 swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
202 auto __tmp = static_cast<remove_reference_t<_Tp>&&>(__t);
203 __t = static_cast<remove_reference_t<_Tp>&&>(__u);
204 __u = static_cast<remove_reference_t<_Tp>&&>(__tmp);
208 template<typename _Tp, typename _Up, size_t _Num>
209 requires requires(const _Swap& __swap, _Tp& __e1, _Up& __e2) {
213 operator()(_Tp (&__e1)[_Num], _Up (&__e2)[_Num]) const
214 noexcept(noexcept(std::declval<const _Swap&>()(*__e1, *__e2)))
216 for (size_t __n = 0; __n < _Num; ++__n)
217 (*this)(__e1[__n], __e2[__n]);
220 } // namespace __cust_swap
222 inline namespace __cust
224 inline constexpr __cust_swap::_Swap swap{};
225 } // inline namespace __cust
226 } // namespace ranges
228 template<typename _Tp>
230 = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
232 template<typename _Tp, typename _Up>
233 concept swappable_with = common_reference_with<_Tp, _Up>
234 && requires(_Tp&& __t, _Up&& __u) {
235 ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Tp&&>(__t));
236 ranges::swap(static_cast<_Up&&>(__u), static_cast<_Up&&>(__u));
237 ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u));
238 ranges::swap(static_cast<_Up&&>(__u), static_cast<_Tp&&>(__t));
241 // [concepts.object], Object concepts
243 template<typename _Tp>
244 concept movable = is_object_v<_Tp> && move_constructible<_Tp>
245 && assignable_from<_Tp&, _Tp> && swappable<_Tp>;
247 template<typename _Tp>
248 concept copyable = copy_constructible<_Tp> && movable<_Tp>
249 && assignable_from<_Tp&, const _Tp&>;
251 template<typename _Tp>
252 concept semiregular = copyable<_Tp> && default_constructible<_Tp>;
254 // [concepts.compare], comparison concepts
256 /// [concept.boolean], concept boolean
257 template<typename _Bp>
259 = movable<remove_cvref_t<_Bp>>
260 && requires(__detail::__cref<_Bp> __b1, __detail::__cref<_Bp> __b2,
262 { __b1 } -> convertible_to<bool>;
263 { !__b1 } -> convertible_to<bool>;
264 { __b1 && __b2 } -> same_as<bool>;
265 { __b1 && __a } -> same_as<bool>;
266 { __a && __b2 } -> same_as<bool>;
267 { __b1 || __b2 } -> same_as<bool>;
268 { __b1 || __a } -> same_as<bool>;
269 { __a || __b2 } -> same_as<bool>;
270 { __b1 == __b2 } -> convertible_to<bool>;
271 { __b1 == __a } -> convertible_to<bool>;
272 { __a == __b2 } -> convertible_to<bool>;
273 { __b1 != __b2 } -> convertible_to<bool>;
274 { __b1 != __a } -> convertible_to<bool>;
275 { __a != __b2 } -> convertible_to<bool>;
278 // [concept.equalitycomparable], concept equality_comparable
282 template<typename _Tp, typename _Up>
283 concept __weakly_eq_cmp_with
284 = requires(__detail::__cref<_Tp> __t, __detail::__cref<_Up> __u) {
285 { __t == __u } -> boolean;
286 { __t != __u } -> boolean;
287 { __u == __t } -> boolean;
288 { __u != __t } -> boolean;
290 } // namespace __detail
292 template<typename _Tp>
293 concept equality_comparable = __detail::__weakly_eq_cmp_with<_Tp, _Tp>;
295 template<typename _Tp, typename _Up>
296 concept equality_comparable_with
297 = equality_comparable<_Tp> && equality_comparable<_Up>
298 && common_reference_with<__detail::__cref<_Tp>, __detail::__cref<_Up>>
299 && equality_comparable<common_reference_t<__detail::__cref<_Tp>,
300 __detail::__cref<_Up>>>
301 && __detail::__weakly_eq_cmp_with<_Tp, _Up>;
303 // [concept.totallyordered], concept totally_ordered
304 template<typename _Tp>
305 concept totally_ordered
306 = equality_comparable<_Tp>
307 && requires(__detail::__cref<_Tp> __a, __detail::__cref<_Tp> __b) {
308 { __a < __b } -> boolean;
309 { __a > __b } -> boolean;
310 { __a <= __b } -> boolean;
311 { __a >= __b } -> boolean;
314 template<typename _Tp, typename _Up>
315 concept totally_ordered_with
316 = totally_ordered<_Tp> && totally_ordered<_Up>
317 && common_reference_with<__detail::__cref<_Tp>, __detail::__cref<_Up>>
318 && totally_ordered<common_reference_t<__detail::__cref<_Tp>,
319 __detail::__cref<_Up>>>
320 && equality_comparable_with<_Tp, _Up>
321 && requires(__detail::__cref<_Tp> __t, __detail::__cref<_Up> __u) {
322 { __t < __u } -> boolean;
323 { __t > __u } -> boolean;
324 { __t <= __u } -> boolean;
325 { __t >= __u } -> boolean;
326 { __u < __t } -> boolean;
327 { __u > __t } -> boolean;
328 { __u <= __t } -> boolean;
329 { __u >= __t } -> boolean;
332 template<typename _Tp>
333 concept regular = semiregular<_Tp> && equality_comparable<_Tp>;
335 // [concepts.callable], callable concepts
337 /// [concept.invocable], concept invocable
338 template<typename _Fn, typename... _Args>
339 concept invocable = is_invocable_v<_Fn, _Args...>;
341 /// [concept.regularinvocable], concept regular_invocable
342 template<typename _Fn, typename... _Args>
343 concept regular_invocable = invocable<_Fn, _Args...>;
345 /// [concept.predicate], concept predicate
346 template<typename _Fn, typename... _Args>
347 concept predicate = regular_invocable<_Fn, _Args...>
348 && boolean<invoke_result_t<_Fn, _Args...>>;
350 /// [concept.relation], concept relation
351 template<typename _Rel, typename _Tp, typename _Up>
353 = predicate<_Rel, _Tp, _Tp> && predicate<_Rel, _Up, _Up>
354 && predicate<_Rel, _Tp, _Up> && predicate<_Rel, _Up, _Tp>;
356 /// [concept.equiv], concept equivalence_relation
357 template<typename _Rel, typename _Tp, typename _Up>
358 concept equivalence_relation = relation<_Rel, _Tp, _Up>;
360 /// [concept.strictweakorder], concept strict_weak_order
361 template<typename _Rel, typename _Tp, typename _Up>
362 concept strict_weak_order = relation<_Rel, _Tp, _Up>;
364 _GLIBCXX_END_NAMESPACE_VERSION
368 #endif /* _GLIBCXX_CONCEPTS */