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 <__utility/exchange.h>
18 #include <__utility/forward.h>
19 #include <__utility/move.h>
20 #include <type_traits>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #pragma GCC system_header
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
30 // [concept.swappable]
36 void swap(_Tp&, _Tp&) = delete;
38 template<class _Tp, class _Up>
39 concept __unqualified_swappable_with =
40 (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&
41 requires(_Tp&& __t, _Up&& __u) {
42 swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
47 template<class _Tp, class _Up, size_t _Size>
48 concept __swappable_arrays =
49 !__unqualified_swappable_with<_Tp(&)[_Size], _Up(&)[_Size]> &&
50 extent_v<_Tp> == extent_v<_Up> &&
51 requires(_Tp(& __t)[_Size], _Up(& __u)[_Size], const __fn& __swap) {
52 __swap(__t[0], __u[0]);
56 concept __exchangeable =
57 !__unqualified_swappable_with<_Tp&, _Tp&> &&
58 move_constructible<_Tp> &&
59 assignable_from<_Tp&, _Tp>;
62 // 2.1 `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...
63 // *The name `swap` is used here unqualified.
64 template<class _Tp, class _Up>
65 requires __unqualified_swappable_with<_Tp, _Up>
66 constexpr void operator()(_Tp&& __t, _Up&& __u) const
67 noexcept(noexcept(swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
69 swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
72 // 2.2 Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...
73 template<class _Tp, class _Up, size_t _Size>
74 requires __swappable_arrays<_Tp, _Up, _Size>
75 constexpr void operator()(_Tp(& __t)[_Size], _Up(& __u)[_Size]) const
76 noexcept(noexcept((*this)(*__t, *__u)))
78 // TODO(cjdb): replace with `ranges::swap_ranges`.
79 for (size_t __i = 0; __i < _Size; ++__i) {
80 (*this)(__t[__i], __u[__i]);
84 // 2.3 Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...
85 template<__exchangeable _Tp>
86 constexpr void operator()(_Tp& __x, _Tp& __y) const
87 noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>)
89 __y = _VSTD::exchange(__x, _VSTD::move(__y));
94 inline namespace __cpo {
95 inline constexpr auto swap = __swap::__fn{};
100 concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
102 template<class _Tp, class _Up>
103 concept swappable_with =
104 common_reference_with<_Tp, _Up> &&
105 requires(_Tp&& __t, _Up&& __u) {
106 ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Tp>(__t));
107 ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Up>(__u));
108 ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u));
109 ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Tp>(__t));
112 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
114 _LIBCPP_END_NAMESPACE_STD
116 #endif // _LIBCPP___CONCEPTS_SWAPPABLE_H