1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H
10 #define _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H
11 
12 #include <__config>
13 #include <__cstddef/size_t.h>
14 #include <__type_traits/add_lvalue_reference.h>
15 #include <__type_traits/enable_if.h>
16 #include <__type_traits/integral_constant.h>
17 #include <__type_traits/is_assignable.h>
18 #include <__type_traits/is_constructible.h>
19 #include <__type_traits/is_nothrow_assignable.h>
20 #include <__type_traits/is_nothrow_constructible.h>
21 #include <__type_traits/void_t.h>
22 #include <__utility/declval.h>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 template <class _Tp, class _Up, class = void>
31 inline const bool __is_swappable_with_v = false;
32 
33 template <class _Tp>
34 inline const bool __is_swappable_v = __is_swappable_with_v<_Tp&, _Tp&>;
35 
36 template <class _Tp, class _Up, bool = __is_swappable_with_v<_Tp, _Up> >
37 inline const bool __is_nothrow_swappable_with_v = false;
38 
39 template <class _Tp>
40 inline const bool __is_nothrow_swappable_v = __is_nothrow_swappable_with_v<_Tp&, _Tp&>;
41 
42 #ifndef _LIBCPP_CXX03_LANG
43 template <class _Tp>
44 using __swap_result_t = __enable_if_t<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>;
45 #else
46 template <class>
47 using __swap_result_t = void;
48 #endif
49 
50 template <class _Tp>
51 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __swap_result_t<_Tp> swap(_Tp& __x, _Tp& __y)
52     _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value&& is_nothrow_move_assignable<_Tp>::value);
53 
54 template <class _Tp, size_t _Np, __enable_if_t<__is_swappable_v<_Tp>, int> = 0>
55 inline _LIBCPP_HIDE_FROM_ABI
56 _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable_v<_Tp>);
57 
58 // ALL generic swap overloads MUST already have a declaration available at this point.
59 
60 template <class _Tp, class _Up>
61 inline const bool __is_swappable_with_v<_Tp,
62                                         _Up,
63                                         __void_t<decltype(swap(std::declval<_Tp>(), std::declval<_Up>())),
64                                                  decltype(swap(std::declval<_Up>(), std::declval<_Tp>()))> > = true;
65 
66 #ifndef _LIBCPP_CXX03_LANG // C++03 doesn't have noexcept, so things are never nothrow swappable
67 template <class _Tp, class _Up>
68 inline const bool __is_nothrow_swappable_with_v<_Tp, _Up, true> =
69     noexcept(swap(std::declval<_Tp>(), std::declval<_Up>())) &&
70     noexcept(swap(std::declval<_Up>(), std::declval<_Tp>()));
71 #endif
72 
73 #if _LIBCPP_STD_VER >= 17
74 
75 template <class _Tp, class _Up>
76 inline constexpr bool is_swappable_with_v = __is_swappable_with_v<_Tp, _Up>;
77 
78 template <class _Tp, class _Up>
79 struct _LIBCPP_TEMPLATE_VIS is_swappable_with : bool_constant<is_swappable_with_v<_Tp, _Up>> {};
80 
81 template <class _Tp>
82 inline constexpr bool is_swappable_v =
83     is_swappable_with_v<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp>>;
84 
85 template <class _Tp>
86 struct _LIBCPP_TEMPLATE_VIS is_swappable : bool_constant<is_swappable_v<_Tp>> {};
87 
88 template <class _Tp, class _Up>
89 inline constexpr bool is_nothrow_swappable_with_v = __is_nothrow_swappable_with_v<_Tp, _Up>;
90 
91 template <class _Tp, class _Up>
92 struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable_with : bool_constant<is_nothrow_swappable_with_v<_Tp, _Up>> {};
93 
94 template <class _Tp>
95 inline constexpr bool is_nothrow_swappable_v =
96     is_nothrow_swappable_with_v<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp>>;
97 
98 template <class _Tp>
99 struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable : bool_constant<is_nothrow_swappable_v<_Tp>> {};
100 
101 #endif // _LIBCPP_STD_VER >= 17
102 
103 _LIBCPP_END_NAMESPACE_STD
104 
105 #endif // _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H
106