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