]>
Commit | Line | Data |
---|---|---|
1 | // Move, forward and identity for C++11 + swap -*- C++ -*- | |
2 | ||
3 | // Copyright (C) 2007-2025 Free Software Foundation, Inc. | |
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 | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
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 | ||
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/>. | |
24 | ||
25 | /** @file bits/move.h | |
26 | * This is an internal header file, included by other library headers. | |
27 | * Do not attempt to use it directly. @headername{utility} | |
28 | */ | |
29 | ||
30 | #ifndef _MOVE_H | |
31 | #define _MOVE_H 1 | |
32 | ||
33 | #include <bits/c++config.h> | |
34 | #if __cplusplus < 201103L | |
35 | # include <bits/concept_check.h> | |
36 | #else | |
37 | # include <type_traits> // Brings in std::declval too. | |
38 | #endif | |
39 | ||
40 | namespace std _GLIBCXX_VISIBILITY(default) | |
41 | { | |
42 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
43 | ||
44 | // Used, in C++03 mode too, by allocators, etc. | |
45 | /** | |
46 | * @brief Same as C++11 std::addressof | |
47 | * @ingroup utilities | |
48 | */ | |
49 | template<typename _Tp> | |
50 | __attribute__((__always_inline__)) | |
51 | inline _GLIBCXX_CONSTEXPR _Tp* | |
52 | __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT | |
53 | { return __builtin_addressof(__r); } | |
54 | ||
55 | #if __cplusplus >= 201103L | |
56 | ||
57 | /** | |
58 | * @addtogroup utilities | |
59 | * @{ | |
60 | */ | |
61 | ||
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". | |
67 | * @since C++11 | |
68 | */ | |
69 | template<typename _Tp> | |
70 | [[__nodiscard__,__gnu__::__always_inline__]] | |
71 | constexpr _Tp&& | |
72 | forward(typename std::remove_reference<_Tp>::type& __t) noexcept | |
73 | { return static_cast<_Tp&&>(__t); } | |
74 | ||
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". | |
80 | * @since C++11 | |
81 | */ | |
82 | template<typename _Tp> | |
83 | [[__nodiscard__,__gnu__::__always_inline__]] | |
84 | constexpr _Tp&& | |
85 | forward(typename std::remove_reference<_Tp>::type&& __t) noexcept | |
86 | { | |
87 | static_assert(!std::is_lvalue_reference<_Tp>::value, | |
88 | "std::forward must not be used to convert an rvalue to an lvalue"); | |
89 | return static_cast<_Tp&&>(__t); | |
90 | } | |
91 | ||
92 | #if __glibcxx_forward_like // C++ >= 23 | |
93 | template<typename _Tp, typename _Up> | |
94 | struct __like_impl; // _Tp must be a reference and _Up an lvalue reference | |
95 | ||
96 | template<typename _Tp, typename _Up> | |
97 | struct __like_impl<_Tp&, _Up&> | |
98 | { using type = _Up&; }; | |
99 | ||
100 | template<typename _Tp, typename _Up> | |
101 | struct __like_impl<const _Tp&, _Up&> | |
102 | { using type = const _Up&; }; | |
103 | ||
104 | template<typename _Tp, typename _Up> | |
105 | struct __like_impl<_Tp&&, _Up&> | |
106 | { using type = _Up&&; }; | |
107 | ||
108 | template<typename _Tp, typename _Up> | |
109 | struct __like_impl<const _Tp&&, _Up&> | |
110 | { using type = const _Up&&; }; | |
111 | ||
112 | template<typename _Tp, typename _Up> | |
113 | using __like_t = typename __like_impl<_Tp&&, _Up&>::type; | |
114 | ||
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 | */ | |
122 | template<typename _Tp, typename _Up> | |
123 | [[nodiscard,__gnu__::__always_inline__]] | |
124 | constexpr __like_t<_Tp, _Up> | |
125 | forward_like(_Up&& __x) noexcept | |
126 | { return static_cast<__like_t<_Tp, _Up>>(__x); } | |
127 | #endif | |
128 | ||
129 | /** | |
130 | * @brief Convert a value to an rvalue. | |
131 | * @param __t A thing of arbitrary type. | |
132 | * @return The parameter cast to an rvalue-reference to allow moving it. | |
133 | * @since C++11 | |
134 | */ | |
135 | template<typename _Tp> | |
136 | [[__nodiscard__,__gnu__::__always_inline__]] | |
137 | constexpr typename std::remove_reference<_Tp>::type&& | |
138 | move(_Tp&& __t) noexcept | |
139 | { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } | |
140 | ||
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 | ||
147 | /** | |
148 | * @brief Conditionally convert a value to an rvalue. | |
149 | * @param __x A thing of arbitrary type. | |
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. | |
154 | * @since C++11 | |
155 | */ | |
156 | template<typename _Tp> | |
157 | [[__nodiscard__,__gnu__::__always_inline__]] | |
158 | constexpr | |
159 | __conditional_t<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&> | |
160 | move_if_noexcept(_Tp& __x) noexcept | |
161 | { return std::move(__x); } | |
162 | ||
163 | // declval, from type_traits. | |
164 | ||
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. | |
171 | * @since C++11 | |
172 | */ | |
173 | template<typename _Tp> | |
174 | [[__nodiscard__,__gnu__::__always_inline__]] | |
175 | inline _GLIBCXX17_CONSTEXPR _Tp* | |
176 | addressof(_Tp& __r) noexcept | |
177 | { return __builtin_addressof(__r); } | |
178 | ||
179 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
180 | // 2598. addressof works on temporaries | |
181 | template<typename _Tp> | |
182 | const _Tp* addressof(const _Tp&&) = delete; | |
183 | ||
184 | // C++11 version of std::exchange for internal use. | |
185 | template <typename _Tp, typename _Up = _Tp> | |
186 | _GLIBCXX20_CONSTEXPR | |
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 | ||
195 | /// @} group utilities | |
196 | ||
197 | #define _GLIBCXX_FWDREF(_Tp) _Tp&& | |
198 | #define _GLIBCXX_MOVE(__val) std::move(__val) | |
199 | #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) | |
200 | #else | |
201 | #define _GLIBCXX_FWDREF(_Tp) const _Tp& | |
202 | #define _GLIBCXX_MOVE(__val) (__val) | |
203 | #define _GLIBCXX_FORWARD(_Tp, __val) (__val) | |
204 | #endif | |
205 | ||
206 | /** | |
207 | * @addtogroup utilities | |
208 | * @{ | |
209 | */ | |
210 | ||
211 | /** | |
212 | * @brief Swaps two values. | |
213 | * @param __a A thing of arbitrary type. | |
214 | * @param __b Another thing of arbitrary type. | |
215 | * @return Nothing. | |
216 | */ | |
217 | template<typename _Tp> | |
218 | _GLIBCXX20_CONSTEXPR | |
219 | inline | |
220 | #if __cplusplus >= 201103L | |
221 | typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>, | |
222 | is_move_constructible<_Tp>, | |
223 | is_move_assignable<_Tp>>::value>::type | |
224 | #else | |
225 | void | |
226 | #endif | |
227 | swap(_Tp& __a, _Tp& __b) | |
228 | _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>, | |
229 | is_nothrow_move_assignable<_Tp>>::value) | |
230 | { | |
231 | #if __cplusplus < 201103L | |
232 | // concept requirements | |
233 | __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) | |
234 | #endif | |
235 | _Tp __tmp = _GLIBCXX_MOVE(__a); | |
236 | __a = _GLIBCXX_MOVE(__b); | |
237 | __b = _GLIBCXX_MOVE(__tmp); | |
238 | } | |
239 | ||
240 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
241 | // DR 809. std::swap should be overloaded for array types. | |
242 | /// Swap the contents of two arrays. | |
243 | template<typename _Tp, size_t _Nm> | |
244 | _GLIBCXX20_CONSTEXPR | |
245 | inline | |
246 | #if __cplusplus >= 201103L | |
247 | typename enable_if<__is_swappable<_Tp>::value>::type | |
248 | #else | |
249 | void | |
250 | #endif | |
251 | swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) | |
252 | _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value) | |
253 | { | |
254 | for (size_t __n = 0; __n < _Nm; ++__n) | |
255 | swap(__a[__n], __b[__n]); | |
256 | } | |
257 | ||
258 | /// @} group utilities | |
259 | _GLIBCXX_END_NAMESPACE_VERSION | |
260 | } // namespace | |
261 | ||
262 | #endif /* _MOVE_H */ |