| 1 | // <concepts> -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2019-2023 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 | |
| 46 | namespace 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 { 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 | namespace __cust_swap |
| 182 | { |
| 183 | template<typename _Tp> void swap(_Tp&, _Tp&) = delete; |
| 184 | |
| 185 | template<typename _Tp, typename _Up> |
| 186 | concept __adl_swap |
| 187 | = (__detail::__class_or_enum<remove_reference_t<_Tp>> |
| 188 | || __detail::__class_or_enum<remove_reference_t<_Up>>) |
| 189 | && requires(_Tp&& __t, _Up&& __u) { |
| 190 | swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); |
| 191 | }; |
| 192 | |
| 193 | struct _Swap |
| 194 | { |
| 195 | private: |
| 196 | template<typename _Tp, typename _Up> |
| 197 | static constexpr bool |
| 198 | _S_noexcept() |
| 199 | { |
| 200 | if constexpr (__adl_swap<_Tp, _Up>) |
| 201 | return noexcept(swap(std::declval<_Tp>(), std::declval<_Up>())); |
| 202 | else |
| 203 | return is_nothrow_move_constructible_v<remove_reference_t<_Tp>> |
| 204 | && is_nothrow_move_assignable_v<remove_reference_t<_Tp>>; |
| 205 | } |
| 206 | |
| 207 | public: |
| 208 | template<typename _Tp, typename _Up> |
| 209 | requires __adl_swap<_Tp, _Up> |
| 210 | || (same_as<_Tp, _Up> && is_lvalue_reference_v<_Tp> |
| 211 | && move_constructible<remove_reference_t<_Tp>> |
| 212 | && assignable_from<_Tp, remove_reference_t<_Tp>>) |
| 213 | constexpr void |
| 214 | operator()(_Tp&& __t, _Up&& __u) const |
| 215 | noexcept(_S_noexcept<_Tp, _Up>()) |
| 216 | { |
| 217 | if constexpr (__adl_swap<_Tp, _Up>) |
| 218 | swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); |
| 219 | else |
| 220 | { |
| 221 | auto __tmp = static_cast<remove_reference_t<_Tp>&&>(__t); |
| 222 | __t = static_cast<remove_reference_t<_Tp>&&>(__u); |
| 223 | __u = static_cast<remove_reference_t<_Tp>&&>(__tmp); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | template<typename _Tp, typename _Up, size_t _Num> |
| 228 | requires requires(const _Swap& __swap, _Tp& __e1, _Up& __e2) { |
| 229 | __swap(__e1, __e2); |
| 230 | } |
| 231 | constexpr void |
| 232 | operator()(_Tp (&__e1)[_Num], _Up (&__e2)[_Num]) const |
| 233 | noexcept(noexcept(std::declval<const _Swap&>()(*__e1, *__e2))) |
| 234 | { |
| 235 | for (size_t __n = 0; __n < _Num; ++__n) |
| 236 | (*this)(__e1[__n], __e2[__n]); |
| 237 | } |
| 238 | }; |
| 239 | } // namespace __cust_swap |
| 240 | |
| 241 | inline namespace __cust |
| 242 | { |
| 243 | inline constexpr __cust_swap::_Swap swap{}; |
| 244 | } // inline namespace __cust |
| 245 | } // namespace ranges |
| 246 | |
| 247 | template<typename _Tp> |
| 248 | concept swappable |
| 249 | = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); }; |
| 250 | |
| 251 | template<typename _Tp, typename _Up> |
| 252 | concept swappable_with = common_reference_with<_Tp, _Up> |
| 253 | && requires(_Tp&& __t, _Up&& __u) { |
| 254 | ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Tp&&>(__t)); |
| 255 | ranges::swap(static_cast<_Up&&>(__u), static_cast<_Up&&>(__u)); |
| 256 | ranges::swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); |
| 257 | ranges::swap(static_cast<_Up&&>(__u), static_cast<_Tp&&>(__t)); |
| 258 | }; |
| 259 | |
| 260 | // [concepts.object], Object concepts |
| 261 | |
| 262 | template<typename _Tp> |
| 263 | concept movable = is_object_v<_Tp> && move_constructible<_Tp> |
| 264 | && assignable_from<_Tp&, _Tp> && swappable<_Tp>; |
| 265 | |
| 266 | template<typename _Tp> |
| 267 | concept copyable = copy_constructible<_Tp> && movable<_Tp> |
| 268 | && assignable_from<_Tp&, _Tp&> && assignable_from<_Tp&, const _Tp&> |
| 269 | && assignable_from<_Tp&, const _Tp>; |
| 270 | |
| 271 | template<typename _Tp> |
| 272 | concept semiregular = copyable<_Tp> && default_initializable<_Tp>; |
| 273 | |
| 274 | // [concepts.compare], comparison concepts |
| 275 | |
| 276 | // [concept.booleantestable], Boolean testability |
| 277 | namespace __detail |
| 278 | { |
| 279 | template<typename _Tp> |
| 280 | concept __boolean_testable_impl = convertible_to<_Tp, bool>; |
| 281 | |
| 282 | template<typename _Tp> |
| 283 | concept __boolean_testable |
| 284 | = __boolean_testable_impl<_Tp> |
| 285 | && requires(_Tp&& __t) |
| 286 | { { !static_cast<_Tp&&>(__t) } -> __boolean_testable_impl; }; |
| 287 | } // namespace __detail |
| 288 | |
| 289 | // [concept.equalitycomparable], concept equality_comparable |
| 290 | |
| 291 | namespace __detail |
| 292 | { |
| 293 | template<typename _Tp, typename _Up> |
| 294 | concept __weakly_eq_cmp_with |
| 295 | = requires(__detail::__cref<_Tp> __t, __detail::__cref<_Up> __u) { |
| 296 | { __t == __u } -> __boolean_testable; |
| 297 | { __t != __u } -> __boolean_testable; |
| 298 | { __u == __t } -> __boolean_testable; |
| 299 | { __u != __t } -> __boolean_testable; |
| 300 | }; |
| 301 | } // namespace __detail |
| 302 | |
| 303 | template<typename _Tp> |
| 304 | concept equality_comparable = __detail::__weakly_eq_cmp_with<_Tp, _Tp>; |
| 305 | |
| 306 | template<typename _Tp, typename _Up> |
| 307 | concept equality_comparable_with |
| 308 | = equality_comparable<_Tp> && equality_comparable<_Up> |
| 309 | && common_reference_with<__detail::__cref<_Tp>, __detail::__cref<_Up>> |
| 310 | && equality_comparable<common_reference_t<__detail::__cref<_Tp>, |
| 311 | __detail::__cref<_Up>>> |
| 312 | && __detail::__weakly_eq_cmp_with<_Tp, _Up>; |
| 313 | |
| 314 | namespace __detail |
| 315 | { |
| 316 | template<typename _Tp, typename _Up> |
| 317 | concept __partially_ordered_with |
| 318 | = requires(const remove_reference_t<_Tp>& __t, |
| 319 | const remove_reference_t<_Up>& __u) { |
| 320 | { __t < __u } -> __boolean_testable; |
| 321 | { __t > __u } -> __boolean_testable; |
| 322 | { __t <= __u } -> __boolean_testable; |
| 323 | { __t >= __u } -> __boolean_testable; |
| 324 | { __u < __t } -> __boolean_testable; |
| 325 | { __u > __t } -> __boolean_testable; |
| 326 | { __u <= __t } -> __boolean_testable; |
| 327 | { __u >= __t } -> __boolean_testable; |
| 328 | }; |
| 329 | } // namespace __detail |
| 330 | |
| 331 | // [concept.totallyordered], concept totally_ordered |
| 332 | template<typename _Tp> |
| 333 | concept totally_ordered |
| 334 | = equality_comparable<_Tp> |
| 335 | && __detail::__partially_ordered_with<_Tp, _Tp>; |
| 336 | |
| 337 | template<typename _Tp, typename _Up> |
| 338 | concept totally_ordered_with |
| 339 | = totally_ordered<_Tp> && totally_ordered<_Up> |
| 340 | && equality_comparable_with<_Tp, _Up> |
| 341 | && totally_ordered<common_reference_t<__detail::__cref<_Tp>, |
| 342 | __detail::__cref<_Up>>> |
| 343 | && __detail::__partially_ordered_with<_Tp, _Up>; |
| 344 | |
| 345 | template<typename _Tp> |
| 346 | concept regular = semiregular<_Tp> && equality_comparable<_Tp>; |
| 347 | |
| 348 | // [concepts.callable], callable concepts |
| 349 | |
| 350 | /// [concept.invocable], concept invocable |
| 351 | template<typename _Fn, typename... _Args> |
| 352 | concept invocable = is_invocable_v<_Fn, _Args...>; |
| 353 | |
| 354 | /// [concept.regularinvocable], concept regular_invocable |
| 355 | template<typename _Fn, typename... _Args> |
| 356 | concept regular_invocable = invocable<_Fn, _Args...>; |
| 357 | |
| 358 | /// [concept.predicate], concept predicate |
| 359 | template<typename _Fn, typename... _Args> |
| 360 | concept predicate = regular_invocable<_Fn, _Args...> |
| 361 | && __detail::__boolean_testable<invoke_result_t<_Fn, _Args...>>; |
| 362 | |
| 363 | /// [concept.relation], concept relation |
| 364 | template<typename _Rel, typename _Tp, typename _Up> |
| 365 | concept relation |
| 366 | = predicate<_Rel, _Tp, _Tp> && predicate<_Rel, _Up, _Up> |
| 367 | && predicate<_Rel, _Tp, _Up> && predicate<_Rel, _Up, _Tp>; |
| 368 | |
| 369 | /// [concept.equiv], concept equivalence_relation |
| 370 | template<typename _Rel, typename _Tp, typename _Up> |
| 371 | concept equivalence_relation = relation<_Rel, _Tp, _Up>; |
| 372 | |
| 373 | /// [concept.strictweakorder], concept strict_weak_order |
| 374 | template<typename _Rel, typename _Tp, typename _Up> |
| 375 | concept strict_weak_order = relation<_Rel, _Tp, _Up>; |
| 376 | |
| 377 | _GLIBCXX_END_NAMESPACE_VERSION |
| 378 | } // namespace |
| 379 | #endif // C++2a |
| 380 | |
| 381 | #endif /* _GLIBCXX_CONCEPTS */ |
| 382 | |