1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CONCEPTS_SWAPPABLE_H
10 #define _LIBCPP___CONCEPTS_SWAPPABLE_H
12 #include <__concepts/assignable.h>
13 #include <__concepts/class_or_enum.h>
14 #include <__concepts/common_reference_with.h>
15 #include <__concepts/constructible.h>
17 #include <__cstddef/size_t.h>
18 #include <__type_traits/extent.h>
19 #include <__type_traits/is_nothrow_assignable.h>
20 #include <__type_traits/is_nothrow_constructible.h>
21 #include <__type_traits/remove_cvref.h>
22 #include <__utility/exchange.h>
23 #include <__utility/forward.h>
24 #include <__utility/move.h>
25 #include <__utility/swap.h>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
32 #include <__undef_macros>
34 _LIBCPP_BEGIN_NAMESPACE_STD
36 #if _LIBCPP_STD_VER >= 20
38 // [concept.swappable]
44 void swap(_Tp
&, _Tp
&) = delete;
47 template <class _Tp
, class _Up
>
48 concept __unqualified_swappable_with
=
49 (__class_or_enum
<remove_cvref_t
<_Tp
>> || __class_or_enum
<remove_cvref_t
<_Up
>>) &&
50 requires(_Tp
&& __t
, _Up
&& __u
) {
51 swap(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
));
58 template <class _Tp
, class _Up
, size_t _Size
>
59 concept __swappable_arrays
=
60 !__unqualified_swappable_with
<_Tp (&)[_Size
], _Up (&)[_Size
]> &&
61 extent_v
<_Tp
> == extent_v
<_Up
> &&
62 requires(_Tp (&__t
)[_Size
], _Up (&__u
)[_Size
], const __fn
& __swap
) {
63 __swap(__t
[0], __u
[0]);
68 concept __exchangeable
=
69 !__unqualified_swappable_with
<_Tp
&, _Tp
&> && move_constructible
<_Tp
> && assignable_from
<_Tp
&, _Tp
>;
72 // 2.1 `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...
73 // *The name `swap` is used here unqualified.
74 template <class _Tp
, class _Up
>
75 requires __unqualified_swappable_with
<_Tp
, _Up
>
76 _LIBCPP_HIDE_FROM_ABI
constexpr void operator()(_Tp
&& __t
, _Up
&& __u
) const
77 noexcept(noexcept(swap(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)))) {
78 swap(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
));
81 // 2.2 Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...
82 template <class _Tp
, class _Up
, size_t _Size
>
83 requires __swappable_arrays
<_Tp
, _Up
, _Size
>
84 _LIBCPP_HIDE_FROM_ABI
constexpr void operator()(_Tp (&__t
)[_Size
], _Up (&__u
)[_Size
]) const
85 noexcept(noexcept((*this)(*__t
, *__u
))) {
86 // TODO(cjdb): replace with `ranges::swap_ranges`.
87 for (size_t __i
= 0; __i
< _Size
; ++__i
) {
88 (*this)(__t
[__i
], __u
[__i
]);
92 // 2.3 Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...
93 template <__exchangeable _Tp
>
94 _LIBCPP_HIDE_FROM_ABI
constexpr void operator()(_Tp
& __x
, _Tp
& __y
) const
95 noexcept(is_nothrow_move_constructible_v
<_Tp
> && is_nothrow_move_assignable_v
<_Tp
>) {
96 __y
= std::exchange(__x
, std::move(__y
));
101 inline namespace __cpo
{
102 inline constexpr auto swap
= __swap::__fn
{};
104 } // namespace ranges
107 concept swappable
= requires(_Tp
& __a
, _Tp
& __b
) { ranges::swap(__a
, __b
); };
109 template <class _Tp
, class _Up
>
110 concept swappable_with
= common_reference_with
<_Tp
, _Up
> && requires(_Tp
&& __t
, _Up
&& __u
) {
111 ranges::swap(std::forward
<_Tp
>(__t
), std::forward
<_Tp
>(__t
));
112 ranges::swap(std::forward
<_Up
>(__u
), std::forward
<_Up
>(__u
));
113 ranges::swap(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
));
114 ranges::swap(std::forward
<_Up
>(__u
), std::forward
<_Tp
>(__t
));
117 #endif // _LIBCPP_STD_VER >= 20
119 _LIBCPP_END_NAMESPACE_STD
123 #endif // _LIBCPP___CONCEPTS_SWAPPABLE_H