]>
Commit | Line | Data |
---|---|---|
4f5f9962 | 1 | // Move, forward and identity for C++11 + swap -*- C++ -*- |
1476e59c | 2 | |
a945c346 | 3 | // Copyright (C) 2007-2024 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> |
9e023e33 | 50 | inline _GLIBCXX_CONSTEXPR _Tp* |
5d861bf2 | 51 | __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT |
9e023e33 | 52 | { return __builtin_addressof(__r); } |
882b3d5c | 53 | |
4a15d842 FD |
54 | #if __cplusplus >= 201103L |
55 | ||
13901e4b JW |
56 | /** |
57 | * @addtogroup utilities | |
58 | * @{ | |
59 | */ | |
60 | ||
13901e4b JW |
61 | /** |
62 | * @brief Forward an lvalue. | |
63 | * @return The parameter cast to the specified type. | |
64 | * | |
65 | * This function is used to implement "perfect forwarding". | |
66 | */ | |
4c7aaebf | 67 | template<typename _Tp> |
406f58e1 | 68 | _GLIBCXX_NODISCARD |
a4eeb822 | 69 | constexpr _Tp&& |
5d861bf2 | 70 | forward(typename std::remove_reference<_Tp>::type& __t) noexcept |
4c7aaebf PC |
71 | { return static_cast<_Tp&&>(__t); } |
72 | ||
13901e4b JW |
73 | /** |
74 | * @brief Forward an rvalue. | |
75 | * @return The parameter cast to the specified type. | |
76 | * | |
77 | * This function is used to implement "perfect forwarding". | |
78 | */ | |
4c7aaebf | 79 | template<typename _Tp> |
406f58e1 | 80 | _GLIBCXX_NODISCARD |
a4eeb822 | 81 | constexpr _Tp&& |
5d861bf2 | 82 | forward(typename std::remove_reference<_Tp>::type&& __t) noexcept |
ad269884 | 83 | { |
a11052d9 JW |
84 | static_assert(!std::is_lvalue_reference<_Tp>::value, |
85 | "std::forward must not be used to convert an rvalue to an lvalue"); | |
ad269884 PC |
86 | return static_cast<_Tp&&>(__t); |
87 | } | |
e7f1930f | 88 | |
55cf4f84 JW |
89 | #if __glibcxx_forward_like // C++ >= 23 |
90 | template<typename _Tp, typename _Up> | |
8256d5c0 PP |
91 | struct __like_impl; // _Tp must be a reference and _Up an lvalue reference |
92 | ||
93 | template<typename _Tp, typename _Up> | |
94 | struct __like_impl<_Tp&, _Up&> | |
95 | { using type = _Up&; }; | |
96 | ||
97 | template<typename _Tp, typename _Up> | |
98 | struct __like_impl<const _Tp&, _Up&> | |
99 | { using type = const _Up&; }; | |
100 | ||
101 | template<typename _Tp, typename _Up> | |
102 | struct __like_impl<_Tp&&, _Up&> | |
103 | { using type = _Up&&; }; | |
104 | ||
105 | template<typename _Tp, typename _Up> | |
106 | struct __like_impl<const _Tp&&, _Up&> | |
107 | { using type = const _Up&&; }; | |
d2cb4693 PP |
108 | |
109 | template<typename _Tp, typename _Up> | |
8256d5c0 PP |
110 | using __like_t = typename __like_impl<_Tp&&, _Up&>::type; |
111 | ||
112 | template<typename _Tp, typename _Up> | |
113 | [[nodiscard]] | |
114 | constexpr __like_t<_Tp, _Up> | |
115 | forward_like(_Up&& __x) noexcept | |
116 | { return static_cast<__like_t<_Tp, _Up>>(__x); } | |
55cf4f84 JW |
117 | #endif |
118 | ||
fd9380a6 | 119 | /** |
13901e4b | 120 | * @brief Convert a value to an rvalue. |
fd9380a6 | 121 | * @param __t A thing of arbitrary type. |
13901e4b | 122 | * @return The parameter cast to an rvalue-reference to allow moving it. |
fd9380a6 | 123 | */ |
1476e59c | 124 | template<typename _Tp> |
406f58e1 | 125 | _GLIBCXX_NODISCARD |
a4eeb822 | 126 | constexpr typename std::remove_reference<_Tp>::type&& |
5d861bf2 | 127 | move(_Tp&& __t) noexcept |
e7f1930f | 128 | { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } |
1476e59c | 129 | |
74a2a1b4 PC |
130 | |
131 | template<typename _Tp> | |
132 | struct __move_if_noexcept_cond | |
133 | : public __and_<__not_<is_nothrow_move_constructible<_Tp>>, | |
134 | is_copy_constructible<_Tp>>::type { }; | |
135 | ||
1f428429 | 136 | /** |
13901e4b | 137 | * @brief Conditionally convert a value to an rvalue. |
1f428429 | 138 | * @param __x A thing of arbitrary type. |
13901e4b JW |
139 | * @return The parameter, possibly cast to an rvalue-reference. |
140 | * | |
141 | * Same as std::move unless the type's move constructor could throw and the | |
142 | * type is copyable, in which case an lvalue-reference is returned instead. | |
1f428429 PC |
143 | */ |
144 | template<typename _Tp> | |
406f58e1 | 145 | _GLIBCXX_NODISCARD |
a09bb4a8 JW |
146 | constexpr |
147 | __conditional_t<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&> | |
1f428429 PC |
148 | move_if_noexcept(_Tp& __x) noexcept |
149 | { return std::move(__x); } | |
150 | ||
13901e4b | 151 | // declval, from type_traits. |
7274deff | 152 | |
882b3d5c PC |
153 | /** |
154 | * @brief Returns the actual address of the object or function | |
155 | * referenced by r, even in the presence of an overloaded | |
156 | * operator&. | |
157 | * @param __r Reference to an object or function. | |
158 | * @return The actual address. | |
159 | */ | |
160 | template<typename _Tp> | |
406f58e1 | 161 | _GLIBCXX_NODISCARD |
9e023e33 | 162 | inline _GLIBCXX17_CONSTEXPR _Tp* |
5d861bf2 | 163 | addressof(_Tp& __r) noexcept |
882b3d5c PC |
164 | { return std::__addressof(__r); } |
165 | ||
3ce96851 JW |
166 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
167 | // 2598. addressof works on temporaries | |
168 | template<typename _Tp> | |
169 | const _Tp* addressof(const _Tp&&) = delete; | |
170 | ||
9b817548 JW |
171 | // C++11 version of std::exchange for internal use. |
172 | template <typename _Tp, typename _Up = _Tp> | |
3a66e68a | 173 | _GLIBCXX20_CONSTEXPR |
9b817548 JW |
174 | inline _Tp |
175 | __exchange(_Tp& __obj, _Up&& __new_val) | |
176 | { | |
177 | _Tp __old_val = std::move(__obj); | |
178 | __obj = std::forward<_Up>(__new_val); | |
179 | return __old_val; | |
180 | } | |
181 | ||
13901e4b | 182 | /// @} group utilities |
1476e59c | 183 | |
90bf60c3 | 184 | #define _GLIBCXX_FWDREF(_Tp) _Tp&& |
5f1fd346 | 185 | #define _GLIBCXX_MOVE(__val) std::move(__val) |
55dd8445 | 186 | #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) |
d98f312c | 187 | #else |
90bf60c3 | 188 | #define _GLIBCXX_FWDREF(_Tp) const _Tp& |
5f1fd346 | 189 | #define _GLIBCXX_MOVE(__val) (__val) |
55dd8445 | 190 | #define _GLIBCXX_FORWARD(_Tp, __val) (__val) |
1476e59c PC |
191 | #endif |
192 | ||
13901e4b JW |
193 | /** |
194 | * @addtogroup utilities | |
195 | * @{ | |
196 | */ | |
197 | ||
e14e932b PC |
198 | /** |
199 | * @brief Swaps two values. | |
fd9380a6 BK |
200 | * @param __a A thing of arbitrary type. |
201 | * @param __b Another thing of arbitrary type. | |
e14e932b PC |
202 | * @return Nothing. |
203 | */ | |
204 | template<typename _Tp> | |
7a91c710 | 205 | _GLIBCXX20_CONSTEXPR |
ddb63209 | 206 | inline |
734f5023 | 207 | #if __cplusplus >= 201103L |
a2863bde VV |
208 | typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>, |
209 | is_move_constructible<_Tp>, | |
ddb63209 | 210 | is_move_assignable<_Tp>>::value>::type |
ddb63209 VV |
211 | #else |
212 | void | |
173f26ae | 213 | #endif |
81912fb3 | 214 | swap(_Tp& __a, _Tp& __b) |
315f8b5f JW |
215 | _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>, |
216 | is_nothrow_move_assignable<_Tp>>::value) | |
e14e932b | 217 | { |
6d0dff49 | 218 | #if __cplusplus < 201103L |
e14e932b PC |
219 | // concept requirements |
220 | __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) | |
6d0dff49 | 221 | #endif |
e14e932b PC |
222 | _Tp __tmp = _GLIBCXX_MOVE(__a); |
223 | __a = _GLIBCXX_MOVE(__b); | |
224 | __b = _GLIBCXX_MOVE(__tmp); | |
225 | } | |
226 | ||
caa8b3c6 PC |
227 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
228 | // DR 809. std::swap should be overloaded for array types. | |
13901e4b | 229 | /// Swap the contents of two arrays. |
caa8b3c6 | 230 | template<typename _Tp, size_t _Nm> |
7a91c710 | 231 | _GLIBCXX20_CONSTEXPR |
ddb63209 | 232 | inline |
734f5023 | 233 | #if __cplusplus >= 201103L |
26b5ace7 | 234 | typename enable_if<__is_swappable<_Tp>::value>::type |
ddb63209 VV |
235 | #else |
236 | void | |
ccb4f5a7 | 237 | #endif |
81912fb3 JW |
238 | swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) |
239 | _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value) | |
caa8b3c6 PC |
240 | { |
241 | for (size_t __n = 0; __n < _Nm; ++__n) | |
242 | swap(__a[__n], __b[__n]); | |
243 | } | |
244 | ||
13901e4b | 245 | /// @} group utilities |
12ffa228 BK |
246 | _GLIBCXX_END_NAMESPACE_VERSION |
247 | } // namespace | |
e14e932b | 248 | |
caa8b3c6 | 249 | #endif /* _MOVE_H */ |