1// <concepts> -*- C++ -*- 2 3// Copyright (C) 2019-2020 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#if __cplusplus > 201703L && __cpp_concepts >= 201907L 34 35#pragma GCC system_header 36 37/** 38 * @defgroup concepts Concepts 39 * @ingroup utilities 40 * 41 * Concepts for checking type requirements. 42 */ 43 44#include <type_traits> 45 46namespace std _GLIBCXX_VISIBILITY(default) 47{ 48_GLIBCXX_BEGIN_NAMESPACE_VERSION 49 50#define __cpp_lib_concepts 202002L 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(add_rvalue_reference_t<_From> (&__f)()) { 74 static_cast<_To>(__f()); 75 }; 76 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>>; 83 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>>>; 98 99 // [concepts.arithmetic], arithmetic concepts 100 101 template<typename _Tp> 102 concept integral = is_integral_v<_Tp>; 103 104 template<typename _Tp> 105 concept signed_integral = integral<_Tp> && is_signed_v<_Tp>; 106 107 template<typename _Tp> 108 concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>; 109 110 template<typename _Tp> 111 concept floating_point = is_floating_point_v<_Tp>; 112 113 namespace __detail 114 { 115 template<typename _Tp> 116 using __cref = const remove_reference_t<_Tp>&; 117 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 122 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>; 130 }; 131 132 /// [concept.destructible], concept destructible 133 template<typename _Tp> 134 concept destructible = is_nothrow_destructible_v<_Tp>; 135 136 /// [concept.constructible], concept constructible_from 137 template<typename _Tp, typename... _Args> 138 concept constructible_from 139 = destructible<_Tp> && is_constructible_v<_Tp, _Args...>; 140 141 /// [concept.defaultinitializable], concept default_initializable 142 template<typename _Tp> 143 concept default_initializable = constructible_from<_Tp> 144 && requires 145 { 146 _Tp{}; 147 (void) ::new _Tp; 148 }; 149 150 /// [concept.moveconstructible], concept move_constructible 151 template<typename _Tp> 152 concept move_constructible 153 = constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>; 154 155 /// [concept.copyconstructible], concept copy_constructible 156 template<typename _Tp> 157 concept copy_constructible 158 = move_constructible<_Tp> 159 && constructible_from<_Tp, _Tp&> && convertible_to<_Tp&, _Tp> 160 && constructible_from<_Tp, const _Tp&> && convertible_to<const _Tp&, _Tp> 161 && constructible_from<_Tp, const _Tp> && convertible_to<const _Tp, _Tp>; 162 163 // [concept.swappable], concept swappable 164 165 namespace ranges 166 { 167 namespace __cust_swap 168 { 169 template<typename _Tp> void swap(_Tp&, _Tp&) = delete; 170 171 template<typename _Tp, typename _Up> 172 concept __adl_swap 173 = (__detail::__class_or_enum<remove_reference_t<_Tp>> 174 || __detail::__class_or_enum<remove_reference_t<_Up>>) 175 && requires(_Tp&& __t, _Up&& __u) { 176 swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); 177 }; 178 179 struct _Swap 180 { 181 private: 182 template<typename _Tp, typename _Up> 183 static constexpr bool 184 _S_noexcept() 185 { 186 if constexpr (__adl_swap<_Tp, _Up>) 187 return noexcept(swap(std::declval<_Tp>(), std::declval<_Up>())); 188 else 189 return is_nothrow_move_constructible_v<remove_reference_t<_Tp>> 190 && is_nothrow_move_assignable_v<remove_reference_t<_Tp>>; 191 } 192 193 public: 194 template<typename _Tp, typename _Up> 195 requires __adl_swap<_Tp, _Up> 196 || (same_as<_Tp, _Up> && is_lvalue_reference_v<_Tp> 197 && move_constructible<remove_reference_t<_Tp>> 198 && assignable_from<_Tp, remove_reference_t<_Tp>>) 199 constexpr void 200 operator()(_Tp&& __t, _Up&& __u) const 201 noexcept(_S_noexcept<_Tp, _Up>()) 202 { 203 if constexpr (__adl_swap<_Tp, _Up>) 204 swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); 205 else 206 { 207 auto __tmp = static_cast<remove_reference_t<_Tp>&&>(__t); 208 __t = static_cast<remove_reference_t<_Tp>&&>(__u); 209 __u = static_cast<remove_reference_t<_Tp>&&>(__tmp); 210 } 211 } 212 213 template<typename _Tp, typename _Up, size_t _Num> 214 requires requires(const _Swap& __swap, _Tp& __e1, _Up& __e2) { 215 __swap(__e1, __e2); 216 } 217 constexpr void 218 operator()(_Tp (&__e1)[_Num], _Up (&__e2)[_Num]) const 219 noexcept(noexcept(std::declval<const _Swap&>()(*__e1, *__e2))) 220 { 221 for (size_t __n = 0; __n < _Num; ++__n) 222 (*this)(__e1[__n], __e2[__n]); 223 } 224 }; 225 } // namespace __cust_swap 226 227 inline namespace __cust 228 { 229 inline constexpr __cust_swap::_Swap swap{}; 230 } // inline namespace __cust 231 } // namespace ranges 232 233 template<typename _Tp> 234 concept swappable 235 = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); }; 236 237 template<typename _Tp, typename _Up> 238 concept swappable_with = common_reference_with<_Tp, _Up> 239 && requires(_Tp&& __t, _Up&& __u) { 240 ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Tp&&>(__t)); 241 ranges::swap(static_cast<_Up&&>(__u), static_cast<_Up&&>(__u)); 242 ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); 243 ranges::swap(static_cast<_Up&&>(__u), static_cast<_Tp&&>(__t)); 244 }; 245 246 // [concepts.object], Object concepts 247 248 template<typename _Tp> 249 concept movable = is_object_v<_Tp> && move_constructible<_Tp> 250 && assignable_from<_Tp&, _Tp> && swappable<_Tp>; 251 252 template<typename _Tp> 253 concept copyable = copy_constructible<_Tp> && movable<_Tp> 254 && assignable_from<_Tp&, _Tp&> && assignable_from<_Tp&, const _Tp&> 255 && assignable_from<_Tp&, const _Tp>; 256 257 template<typename _Tp> 258 concept semiregular = copyable<_Tp> && default_initializable<_Tp>; 259 260 // [concepts.compare], comparison concepts 261 262 // [concept.booleantestable], Boolean testability 263 namespace __detail 264 { 265 template<typename _Tp> 266 concept __boolean_testable_impl = convertible_to<_Tp, bool>; 267 268 template<typename _Tp> 269 concept __boolean_testable 270 = __boolean_testable_impl<_Tp> 271 && requires(_Tp&& __t) 272 { { !static_cast<_Tp&&>(__t) } -> __boolean_testable_impl; }; 273 } // namespace __detail 274 275 // [concept.equalitycomparable], concept equality_comparable 276 277 namespace __detail 278 { 279 template<typename _Tp, typename _Up> 280 concept __weakly_eq_cmp_with 281 = requires(__detail::__cref<_Tp> __t, __detail::__cref<_Up> __u) { 282 { __t == __u } -> __boolean_testable; 283 { __t != __u } -> __boolean_testable; 284 { __u == __t } -> __boolean_testable; 285 { __u != __t } -> __boolean_testable; 286 }; 287 } // namespace __detail 288 289 template<typename _Tp> 290 concept equality_comparable = __detail::__weakly_eq_cmp_with<_Tp, _Tp>; 291 292 template<typename _Tp, typename _Up> 293 concept equality_comparable_with 294 = equality_comparable<_Tp> && equality_comparable<_Up> 295 && common_reference_with<__detail::__cref<_Tp>, __detail::__cref<_Up>> 296 && equality_comparable<common_reference_t<__detail::__cref<_Tp>, 297 __detail::__cref<_Up>>> 298 && __detail::__weakly_eq_cmp_with<_Tp, _Up>; 299 300 namespace __detail 301 { 302 template<typename _Tp, typename _Up> 303 concept __partially_ordered_with 304 = requires(const remove_reference_t<_Tp>& __t, 305 const remove_reference_t<_Up>& __u) { 306 { __t < __u } -> __boolean_testable; 307 { __t > __u } -> __boolean_testable; 308 { __t <= __u } -> __boolean_testable; 309 { __t >= __u } -> __boolean_testable; 310 { __u < __t } -> __boolean_testable; 311 { __u > __t } -> __boolean_testable; 312 { __u <= __t } -> __boolean_testable; 313 { __u >= __t } -> __boolean_testable; 314 }; 315 } // namespace __detail 316 317 // [concept.totallyordered], concept totally_ordered 318 template<typename _Tp> 319 concept totally_ordered 320 = equality_comparable<_Tp> 321 && __detail::__partially_ordered_with<_Tp, _Tp>; 322 323 template<typename _Tp, typename _Up> 324 concept totally_ordered_with 325 = totally_ordered<_Tp> && totally_ordered<_Up> 326 && equality_comparable_with<_Tp, _Up> 327 && totally_ordered<common_reference_t<__detail::__cref<_Tp>, 328 __detail::__cref<_Up>>> 329 && __detail::__partially_ordered_with<_Tp, _Up>; 330 331 template<typename _Tp> 332 concept regular = semiregular<_Tp> && equality_comparable<_Tp>; 333 334 // [concepts.callable], callable concepts 335 336 /// [concept.invocable], concept invocable 337 template<typename _Fn, typename... _Args> 338 concept invocable = is_invocable_v<_Fn, _Args...>; 339 340 /// [concept.regularinvocable], concept regular_invocable 341 template<typename _Fn, typename... _Args> 342 concept regular_invocable = invocable<_Fn, _Args...>; 343 344 /// [concept.predicate], concept predicate 345 template<typename _Fn, typename... _Args> 346 concept predicate = regular_invocable<_Fn, _Args...> 347 && __detail::__boolean_testable<invoke_result_t<_Fn, _Args...>>; 348 349 /// [concept.relation], concept relation 350 template<typename _Rel, typename _Tp, typename _Up> 351 concept relation 352 = predicate<_Rel, _Tp, _Tp> && predicate<_Rel, _Up, _Up> 353 && predicate<_Rel, _Tp, _Up> && predicate<_Rel, _Up, _Tp>; 354 355 /// [concept.equiv], concept equivalence_relation 356 template<typename _Rel, typename _Tp, typename _Up> 357 concept equivalence_relation = relation<_Rel, _Tp, _Up>; 358 359 /// [concept.strictweakorder], concept strict_weak_order 360 template<typename _Rel, typename _Tp, typename _Up> 361 concept strict_weak_order = relation<_Rel, _Tp, _Up>; 362 363_GLIBCXX_END_NAMESPACE_VERSION 364} // namespace 365#endif // C++2a 366 367#endif /* _GLIBCXX_CONCEPTS */ 368