xref: /llvm-project/libcxx/include/__type_traits/is_swappable.h (revision 6f684816e25d8b4e5fb2cbc7d0560d608a8bd938)
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 _LIBCPP_NODEBUG =
45     __enable_if_t<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>;
46 #else
47 template <class>
48 using __swap_result_t _LIBCPP_NODEBUG = void;
49 #endif
50 
51 template <class _Tp>
52 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __swap_result_t<_Tp> swap(_Tp& __x, _Tp& __y)
53     _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value&& is_nothrow_move_assignable<_Tp>::value);
54 
55 template <class _Tp, size_t _Np, __enable_if_t<__is_swappable_v<_Tp>, int> = 0>
56 inline _LIBCPP_HIDE_FROM_ABI
57 _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable_v<_Tp>);
58 
59 // ALL generic swap overloads MUST already have a declaration available at this point.
60 
61 template <class _Tp, class _Up>
62 inline const bool __is_swappable_with_v<_Tp,
63                                         _Up,
64                                         __void_t<decltype(swap(std::declval<_Tp>(), std::declval<_Up>())),
65                                                  decltype(swap(std::declval<_Up>(), std::declval<_Tp>()))> > = true;
66 
67 #ifndef _LIBCPP_CXX03_LANG // C++03 doesn't have noexcept, so things are never nothrow swappable
68 template <class _Tp, class _Up>
69 inline const bool __is_nothrow_swappable_with_v<_Tp, _Up, true> =
70     noexcept(swap(std::declval<_Tp>(), std::declval<_Up>())) &&
71     noexcept(swap(std::declval<_Up>(), std::declval<_Tp>()));
72 #endif
73 
74 #if _LIBCPP_STD_VER >= 17
75 
76 template <class _Tp, class _Up>
77 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_swappable_with_v = __is_swappable_with_v<_Tp, _Up>;
78 
79 template <class _Tp, class _Up>
80 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_swappable_with
81     : bool_constant<is_swappable_with_v<_Tp, _Up>> {};
82 
83 template <class _Tp>
84 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_swappable_v =
85     is_swappable_with_v<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp>>;
86 
87 template <class _Tp>
88 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_swappable : bool_constant<is_swappable_v<_Tp>> {};
89 
90 template <class _Tp, class _Up>
91 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_swappable_with_v = __is_nothrow_swappable_with_v<_Tp, _Up>;
92 
93 template <class _Tp, class _Up>
94 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_nothrow_swappable_with
95     : bool_constant<is_nothrow_swappable_with_v<_Tp, _Up>> {};
96 
97 template <class _Tp>
98 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_swappable_v =
99     is_nothrow_swappable_with_v<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp>>;
100 
101 template <class _Tp>
102 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS is_nothrow_swappable
103     : bool_constant<is_nothrow_swappable_v<_Tp>> {};
104 
105 #endif // _LIBCPP_STD_VER >= 17
106 
107 _LIBCPP_END_NAMESPACE_STD
108 
109 #endif // _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H
110