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/conditional.h> 15 #include <__type_traits/enable_if.h> 16 #include <__type_traits/is_move_assignable.h> 17 #include <__type_traits/is_move_constructible.h> 18 #include <__type_traits/is_nothrow_move_assignable.h> 19 #include <__type_traits/is_nothrow_move_constructible.h> 20 #include <__type_traits/is_referenceable.h> 21 #include <__type_traits/is_same.h> 22 #include <__type_traits/is_void.h> 23 #include <__type_traits/nat.h> 24 #include <__utility/declval.h> 25 #include <cstddef> 26 27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 28 # pragma GCC system_header 29 #endif 30 31 _LIBCPP_BEGIN_NAMESPACE_STD 32 33 template <class _Tp> 34 struct __is_swappable; 35 template <class _Tp> 36 struct __is_nothrow_swappable; 37 38 #ifndef _LIBCPP_CXX03_LANG 39 template <class _Tp> 40 using __swap_result_t = __enable_if_t<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>; 41 #else 42 template <class> 43 using __swap_result_t = void; 44 #endif 45 46 template <class _Tp> 47 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __swap_result_t<_Tp> swap(_Tp& __x, _Tp& __y) 48 _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value&& is_nothrow_move_assignable<_Tp>::value); 49 50 template <class _Tp, size_t _Np, __enable_if_t<__is_swappable<_Tp>::value, int> = 0> 51 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) 52 _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value); 53 54 namespace __detail { 55 // ALL generic swap overloads MUST already have a declaration available at this point. 56 57 template <class _Tp, class _Up = _Tp, bool _NotVoid = !is_void<_Tp>::value && !is_void<_Up>::value> 58 struct __swappable_with { 59 template <class _LHS, class _RHS> 60 static decltype(swap(std::declval<_LHS>(), std::declval<_RHS>())) __test_swap(int); 61 template <class, class> 62 static __nat __test_swap(long); 63 64 // Extra parens are needed for the C++03 definition of decltype. 65 typedef decltype((__test_swap<_Tp, _Up>(0))) __swap1; 66 typedef decltype((__test_swap<_Up, _Tp>(0))) __swap2; 67 68 static const bool value = _IsNotSame<__swap1, __nat>::value && _IsNotSame<__swap2, __nat>::value; 69 }; 70 71 template <class _Tp, class _Up> 72 struct __swappable_with<_Tp, _Up, false> : false_type {}; 73 74 template <class _Tp, class _Up = _Tp, bool _Swappable = __swappable_with<_Tp, _Up>::value> 75 struct __nothrow_swappable_with { 76 static const bool value = 77 #ifndef _LIBCPP_HAS_NO_NOEXCEPT 78 noexcept(swap(std::declval<_Tp>(), std::declval<_Up>()))&& noexcept( 79 swap(std::declval<_Up>(), std::declval<_Tp>())); 80 #else 81 false; 82 #endif 83 }; 84 85 template <class _Tp, class _Up> 86 struct __nothrow_swappable_with<_Tp, _Up, false> : false_type {}; 87 88 } // namespace __detail 89 90 template <class _Tp> 91 struct __is_swappable : public integral_constant<bool, __detail::__swappable_with<_Tp&>::value> {}; 92 93 template <class _Tp> 94 struct __is_nothrow_swappable : public integral_constant<bool, __detail::__nothrow_swappable_with<_Tp&>::value> {}; 95 96 #if _LIBCPP_STD_VER >= 17 97 98 template <class _Tp, class _Up> 99 struct _LIBCPP_TEMPLATE_VIS is_swappable_with 100 : public integral_constant<bool, __detail::__swappable_with<_Tp, _Up>::value> {}; 101 102 template <class _Tp> 103 struct _LIBCPP_TEMPLATE_VIS is_swappable 104 : public __conditional_t<__libcpp_is_referenceable<_Tp>::value, 105 is_swappable_with<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp> >, 106 false_type> {}; 107 108 template <class _Tp, class _Up> 109 struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable_with 110 : public integral_constant<bool, __detail::__nothrow_swappable_with<_Tp, _Up>::value> {}; 111 112 template <class _Tp> 113 struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable 114 : public __conditional_t<__libcpp_is_referenceable<_Tp>::value, 115 is_nothrow_swappable_with<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp> >, 116 false_type> {}; 117 118 template <class _Tp, class _Up> 119 inline constexpr bool is_swappable_with_v = is_swappable_with<_Tp, _Up>::value; 120 121 template <class _Tp> 122 inline constexpr bool is_swappable_v = is_swappable<_Tp>::value; 123 124 template <class _Tp, class _Up> 125 inline constexpr bool is_nothrow_swappable_with_v = is_nothrow_swappable_with<_Tp, _Up>::value; 126 127 template <class _Tp> 128 inline constexpr bool is_nothrow_swappable_v = is_nothrow_swappable<_Tp>::value; 129 130 #endif // _LIBCPP_STD_VER >= 17 131 132 _LIBCPP_END_NAMESPACE_STD 133 134 #endif // _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H 135