]>
| Commit | Line | Data |
|---|---|---|
| 4f5f9962 | 1 | // Move, forward and identity for C++11 + swap -*- C++ -*- |
| 1476e59c | 2 | |
| 6441eb6d | 3 | // Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 1476e59c PC |
4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free | |
| 6 | // software; you can redistribute it and/or modify it under the | |
| 7 | // terms of the GNU General Public License as published by the | |
| 748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
| 1476e59c PC |
9 | // any later version. |
| 10 | ||
| 11 | // This library is distributed in the hope that it will be useful, | |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | // GNU General Public License for more details. | |
| 15 | ||
| 748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version | |
| 18 | // 3.1, as published by the Free Software Foundation. | |
| 19 | ||
| 20 | // You should have received a copy of the GNU General Public License and | |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; | |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
| 23 | // <http://www.gnu.org/licenses/>. | |
| 1476e59c | 24 | |
| f910786b | 25 | /** @file bits/move.h |
| 1476e59c | 26 | * This is an internal header file, included by other library headers. |
| f910786b | 27 | * Do not attempt to use it directly. @headername{utility} |
| 1476e59c PC |
28 | */ |
| 29 | ||
| caa8b3c6 PC |
30 | #ifndef _MOVE_H |
| 31 | #define _MOVE_H 1 | |
| 1476e59c | 32 | |
| e14e932b | 33 | #include <bits/c++config.h> |
| 6d0dff49 JW |
34 | #if __cplusplus < 201103L |
| 35 | # include <bits/concept_check.h> | |
| 5c8b154c JW |
36 | #else |
| 37 | # include <type_traits> // Brings in std::declval too. | |
| 6d0dff49 | 38 | #endif |
| e14e932b | 39 | |
| 12ffa228 BK |
40 | namespace std _GLIBCXX_VISIBILITY(default) |
| 41 | { | |
| 42 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
| 882b3d5c PC |
43 | |
| 44 | // Used, in C++03 mode too, by allocators, etc. | |
| 13901e4b JW |
45 | /** |
| 46 | * @brief Same as C++11 std::addressof | |
| 47 | * @ingroup utilities | |
| 48 | */ | |
| 882b3d5c | 49 | template<typename _Tp> |
| 96c32a59 | 50 | __attribute__((__always_inline__)) |
| 9e023e33 | 51 | inline _GLIBCXX_CONSTEXPR _Tp* |
| 5d861bf2 | 52 | __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT |
| 9e023e33 | 53 | { return __builtin_addressof(__r); } |
| 882b3d5c | 54 | |
| 4a15d842 FD |
55 | #if __cplusplus >= 201103L |
| 56 | ||
| 13901e4b JW |
57 | /** |
| 58 | * @addtogroup utilities | |
| 59 | * @{ | |
| 60 | */ | |
| 61 | ||
| 13901e4b JW |
62 | /** |
| 63 | * @brief Forward an lvalue. | |
| 64 | * @return The parameter cast to the specified type. | |
| 65 | * | |
| 66 | * This function is used to implement "perfect forwarding". | |
| 4a4e5394 | 67 | * @since C++11 |
| 13901e4b | 68 | */ |
| 4c7aaebf | 69 | template<typename _Tp> |
| 96c32a59 | 70 | [[__nodiscard__,__gnu__::__always_inline__]] |
| a4eeb822 | 71 | constexpr _Tp&& |
| 5d861bf2 | 72 | forward(typename std::remove_reference<_Tp>::type& __t) noexcept |
| 4c7aaebf PC |
73 | { return static_cast<_Tp&&>(__t); } |
| 74 | ||
| 13901e4b JW |
75 | /** |
| 76 | * @brief Forward an rvalue. | |
| 77 | * @return The parameter cast to the specified type. | |
| 78 | * | |
| 79 | * This function is used to implement "perfect forwarding". | |
| 4a4e5394 | 80 | * @since C++11 |
| 13901e4b | 81 | */ |
| 4c7aaebf | 82 | template<typename _Tp> |
| 96c32a59 | 83 | [[__nodiscard__,__gnu__::__always_inline__]] |
| a4eeb822 | 84 | constexpr _Tp&& |
| 5d861bf2 | 85 | forward(typename std::remove_reference<_Tp>::type&& __t) noexcept |
| ad269884 | 86 | { |
| a11052d9 JW |
87 | static_assert(!std::is_lvalue_reference<_Tp>::value, |
| 88 | "std::forward must not be used to convert an rvalue to an lvalue"); | |
| ad269884 PC |
89 | return static_cast<_Tp&&>(__t); |
| 90 | } | |
| e7f1930f | 91 | |
| 55cf4f84 | 92 | template<typename _Tp, typename _Up> |
| 8256d5c0 PP |
93 | struct __like_impl; // _Tp must be a reference and _Up an lvalue reference |
| 94 | ||
| 95 | template<typename _Tp, typename _Up> | |
| 96 | struct __like_impl<_Tp&, _Up&> | |
| 97 | { using type = _Up&; }; | |
| 98 | ||
| 99 | template<typename _Tp, typename _Up> | |
| 100 | struct __like_impl<const _Tp&, _Up&> | |
| 101 | { using type = const _Up&; }; | |
| 102 | ||
| 103 | template<typename _Tp, typename _Up> | |
| 104 | struct __like_impl<_Tp&&, _Up&> | |
| 105 | { using type = _Up&&; }; | |
| 106 | ||
| 107 | template<typename _Tp, typename _Up> | |
| 108 | struct __like_impl<const _Tp&&, _Up&> | |
| 109 | { using type = const _Up&&; }; | |
| d2cb4693 PP |
110 | |
| 111 | template<typename _Tp, typename _Up> | |
| 8256d5c0 PP |
112 | using __like_t = typename __like_impl<_Tp&&, _Up&>::type; |
| 113 | ||
| 250dd5b5 | 114 | #if __glibcxx_forward_like // C++ >= 23 |
| 4a4e5394 JW |
115 | /** @brief Forward with the cv-qualifiers and value category of another type. |
| 116 | * @tparam _Tp An lvalue reference or rvalue reference. | |
| 117 | * @tparam _Up An lvalue reference type deduced from the function argument. | |
| 118 | * @param __x An lvalue. | |
| 119 | * @return `__x` converted to match the qualifiers of `_Tp`. | |
| 120 | * @since C++23 | |
| 121 | */ | |
| 8256d5c0 | 122 | template<typename _Tp, typename _Up> |
| 96c32a59 | 123 | [[nodiscard,__gnu__::__always_inline__]] |
| 8256d5c0 PP |
124 | constexpr __like_t<_Tp, _Up> |
| 125 | forward_like(_Up&& __x) noexcept | |
| 126 | { return static_cast<__like_t<_Tp, _Up>>(__x); } | |
| 55cf4f84 JW |
127 | #endif |
| 128 | ||
| fd9380a6 | 129 | /** |
| 13901e4b | 130 | * @brief Convert a value to an rvalue. |
| fd9380a6 | 131 | * @param __t A thing of arbitrary type. |
| 13901e4b | 132 | * @return The parameter cast to an rvalue-reference to allow moving it. |
| 4a4e5394 | 133 | * @since C++11 |
| fd9380a6 | 134 | */ |
| 1476e59c | 135 | template<typename _Tp> |
| 96c32a59 | 136 | [[__nodiscard__,__gnu__::__always_inline__]] |
| a4eeb822 | 137 | constexpr typename std::remove_reference<_Tp>::type&& |
| 5d861bf2 | 138 | move(_Tp&& __t) noexcept |
| e7f1930f | 139 | { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } |
| 1476e59c | 140 | |
| 74a2a1b4 PC |
141 | |
| 142 | template<typename _Tp> | |
| 143 | struct __move_if_noexcept_cond | |
| 144 | : public __and_<__not_<is_nothrow_move_constructible<_Tp>>, | |
| 145 | is_copy_constructible<_Tp>>::type { }; | |
| 146 | ||
| 1f428429 | 147 | /** |
| 13901e4b | 148 | * @brief Conditionally convert a value to an rvalue. |
| 1f428429 | 149 | * @param __x A thing of arbitrary type. |
| 13901e4b JW |
150 | * @return The parameter, possibly cast to an rvalue-reference. |
| 151 | * | |
| 152 | * Same as std::move unless the type's move constructor could throw and the | |
| 153 | * type is copyable, in which case an lvalue-reference is returned instead. | |
| 4a4e5394 | 154 | * @since C++11 |
| 1f428429 PC |
155 | */ |
| 156 | template<typename _Tp> | |
| 96c32a59 | 157 | [[__nodiscard__,__gnu__::__always_inline__]] |
| a09bb4a8 JW |
158 | constexpr |
| 159 | __conditional_t<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&> | |
| 1f428429 PC |
160 | move_if_noexcept(_Tp& __x) noexcept |
| 161 | { return std::move(__x); } | |
| 162 | ||
| 13901e4b | 163 | // declval, from type_traits. |
| 7274deff | 164 | |
| 882b3d5c PC |
165 | /** |
| 166 | * @brief Returns the actual address of the object or function | |
| 167 | * referenced by r, even in the presence of an overloaded | |
| 168 | * operator&. | |
| 169 | * @param __r Reference to an object or function. | |
| 170 | * @return The actual address. | |
| 4a4e5394 | 171 | * @since C++11 |
| 882b3d5c PC |
172 | */ |
| 173 | template<typename _Tp> | |
| 96c32a59 | 174 | [[__nodiscard__,__gnu__::__always_inline__]] |
| 9e023e33 | 175 | inline _GLIBCXX17_CONSTEXPR _Tp* |
| 5d861bf2 | 176 | addressof(_Tp& __r) noexcept |
| c91eb5a5 | 177 | { return __builtin_addressof(__r); } |
| 882b3d5c | 178 | |
| 3ce96851 JW |
179 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 180 | // 2598. addressof works on temporaries | |
| 181 | template<typename _Tp> | |
| 182 | const _Tp* addressof(const _Tp&&) = delete; | |
| 183 | ||
| 9b817548 JW |
184 | // C++11 version of std::exchange for internal use. |
| 185 | template <typename _Tp, typename _Up = _Tp> | |
| 3a66e68a | 186 | _GLIBCXX20_CONSTEXPR |
| 9b817548 JW |
187 | inline _Tp |
| 188 | __exchange(_Tp& __obj, _Up&& __new_val) | |
| 189 | { | |
| 190 | _Tp __old_val = std::move(__obj); | |
| 191 | __obj = std::forward<_Up>(__new_val); | |
| 192 | return __old_val; | |
| 193 | } | |
| 194 | ||
| 13901e4b | 195 | /// @} group utilities |
| 1476e59c | 196 | |
| 90bf60c3 | 197 | #define _GLIBCXX_FWDREF(_Tp) _Tp&& |
| 5f1fd346 | 198 | #define _GLIBCXX_MOVE(__val) std::move(__val) |
| 55dd8445 | 199 | #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) |
| d98f312c | 200 | #else |
| 90bf60c3 | 201 | #define _GLIBCXX_FWDREF(_Tp) const _Tp& |
| 5f1fd346 | 202 | #define _GLIBCXX_MOVE(__val) (__val) |
| 55dd8445 | 203 | #define _GLIBCXX_FORWARD(_Tp, __val) (__val) |
| 1476e59c PC |
204 | #endif |
| 205 | ||
| 13901e4b JW |
206 | /** |
| 207 | * @addtogroup utilities | |
| 208 | * @{ | |
| 209 | */ | |
| 210 | ||
| e14e932b PC |
211 | /** |
| 212 | * @brief Swaps two values. | |
| fd9380a6 BK |
213 | * @param __a A thing of arbitrary type. |
| 214 | * @param __b Another thing of arbitrary type. | |
| e14e932b PC |
215 | * @return Nothing. |
| 216 | */ | |
| 217 | template<typename _Tp> | |
| 178dc336 JW |
218 | #if __glibcxx_concepts // >= C++20 |
| 219 | requires (! __is_tuple_like<_Tp>::value) | |
| 220 | && is_move_constructible_v<_Tp> | |
| 221 | && is_move_assignable_v<_Tp> | |
| 222 | constexpr void | |
| 223 | #elif __cplusplus >= 201103L | |
| 224 | _GLIBCXX20_CONSTEXPR inline | |
| 225 | __enable_if_t<__and_<__not_<__is_tuple_like<_Tp>>, | |
| 226 | is_move_constructible<_Tp>, | |
| 227 | is_move_assignable<_Tp>>::value> | |
| ddb63209 | 228 | #else |
| 178dc336 | 229 | inline void |
| 173f26ae | 230 | #endif |
| 81912fb3 | 231 | swap(_Tp& __a, _Tp& __b) |
| 315f8b5f JW |
232 | _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>, |
| 233 | is_nothrow_move_assignable<_Tp>>::value) | |
| e14e932b | 234 | { |
| 6d0dff49 | 235 | #if __cplusplus < 201103L |
| e14e932b PC |
236 | // concept requirements |
| 237 | __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) | |
| 6d0dff49 | 238 | #endif |
| e14e932b PC |
239 | _Tp __tmp = _GLIBCXX_MOVE(__a); |
| 240 | __a = _GLIBCXX_MOVE(__b); | |
| 241 | __b = _GLIBCXX_MOVE(__tmp); | |
| 242 | } | |
| 243 | ||
| caa8b3c6 PC |
244 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 245 | // DR 809. std::swap should be overloaded for array types. | |
| 13901e4b | 246 | /// Swap the contents of two arrays. |
| caa8b3c6 | 247 | template<typename _Tp, size_t _Nm> |
| 178dc336 JW |
248 | #if __glibcxx_concepts // >= C++20 |
| 249 | requires is_swappable_v<_Tp> | |
| 250 | constexpr void | |
| 251 | #elif __cplusplus >= 201103L | |
| 252 | _GLIBCXX20_CONSTEXPR inline | |
| 253 | __enable_if_t<__is_swappable<_Tp>::value> | |
| ddb63209 | 254 | #else |
| 178dc336 | 255 | inline void |
| ccb4f5a7 | 256 | #endif |
| 81912fb3 JW |
257 | swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) |
| 258 | _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value) | |
| caa8b3c6 PC |
259 | { |
| 260 | for (size_t __n = 0; __n < _Nm; ++__n) | |
| 261 | swap(__a[__n], __b[__n]); | |
| 262 | } | |
| 263 | ||
| 13901e4b | 264 | /// @} group utilities |
| 12ffa228 BK |
265 | _GLIBCXX_END_NAMESPACE_VERSION |
| 266 | } // namespace | |
| e14e932b | 267 | |
| caa8b3c6 | 268 | #endif /* _MOVE_H */ |