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 = typename enable_if<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>::type; 41 #else 42 template <class> 43 using __swap_result_t = void; 44 #endif 45 46 template <class _Tp> 47 inline _LIBCPP_INLINE_VISIBILITY _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> 51 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 52 typename enable_if<__is_swappable<_Tp>::value>::type swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) 53 _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value); 54 55 namespace __detail { 56 // ALL generic swap overloads MUST already have a declaration available at this point. 57 58 template <class _Tp, class _Up = _Tp, bool _NotVoid = !is_void<_Tp>::value && !is_void<_Up>::value> 59 struct __swappable_with { 60 template <class _LHS, class _RHS> 61 static decltype(swap(std::declval<_LHS>(), std::declval<_RHS>())) __test_swap(int); 62 template <class, class> 63 static __nat __test_swap(long); 64 65 // Extra parens are needed for the C++03 definition of decltype. 66 typedef decltype((__test_swap<_Tp, _Up>(0))) __swap1; 67 typedef decltype((__test_swap<_Up, _Tp>(0))) __swap2; 68 69 static const bool value = _IsNotSame<__swap1, __nat>::value && _IsNotSame<__swap2, __nat>::value; 70 }; 71 72 template <class _Tp, class _Up> 73 struct __swappable_with<_Tp, _Up, false> : false_type {}; 74 75 template <class _Tp, class _Up = _Tp, bool _Swappable = __swappable_with<_Tp, _Up>::value> 76 struct __nothrow_swappable_with { 77 static const bool value = 78 #ifndef _LIBCPP_HAS_NO_NOEXCEPT 79 noexcept(swap(std::declval<_Tp>(), std::declval<_Up>()))&& noexcept( 80 swap(std::declval<_Up>(), std::declval<_Tp>())); 81 #else 82 false; 83 #endif 84 }; 85 86 template <class _Tp, class _Up> 87 struct __nothrow_swappable_with<_Tp, _Up, false> : false_type {}; 88 89 } // namespace __detail 90 91 template <class _Tp> 92 struct __is_swappable : public integral_constant<bool, __detail::__swappable_with<_Tp&>::value> {}; 93 94 template <class _Tp> 95 struct __is_nothrow_swappable : public integral_constant<bool, __detail::__nothrow_swappable_with<_Tp&>::value> {}; 96 97 #if _LIBCPP_STD_VER >= 17 98 99 template <class _Tp, class _Up> 100 struct _LIBCPP_TEMPLATE_VIS is_swappable_with 101 : public integral_constant<bool, __detail::__swappable_with<_Tp, _Up>::value> {}; 102 103 template <class _Tp> 104 struct _LIBCPP_TEMPLATE_VIS is_swappable 105 : public __conditional_t<__libcpp_is_referenceable<_Tp>::value, 106 is_swappable_with<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp> >, 107 false_type> {}; 108 109 template <class _Tp, class _Up> 110 struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable_with 111 : public integral_constant<bool, __detail::__nothrow_swappable_with<_Tp, _Up>::value> {}; 112 113 template <class _Tp> 114 struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable 115 : public __conditional_t<__libcpp_is_referenceable<_Tp>::value, 116 is_nothrow_swappable_with<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp> >, 117 false_type> {}; 118 119 template <class _Tp, class _Up> 120 inline constexpr bool is_swappable_with_v = is_swappable_with<_Tp, _Up>::value; 121 122 template <class _Tp> 123 inline constexpr bool is_swappable_v = is_swappable<_Tp>::value; 124 125 template <class _Tp, class _Up> 126 inline constexpr bool is_nothrow_swappable_with_v = is_nothrow_swappable_with<_Tp, _Up>::value; 127 128 template <class _Tp> 129 inline constexpr bool is_nothrow_swappable_v = is_nothrow_swappable<_Tp>::value; 130 131 #endif // _LIBCPP_STD_VER >= 17 132 133 _LIBCPP_END_NAMESPACE_STD 134 135 #endif // _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H 136