]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/move.h
54606cd4e8f5ac749af106fb2eb23740fa35d3b5
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / move.h
1 // Move, forward and identity for C++11 + swap -*- C++ -*-
2
3 // Copyright (C) 2007-2023 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 inline _GLIBCXX_CONSTEXPR _Tp*
51 __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
52 { return __builtin_addressof(__r); }
53
54 #if __cplusplus >= 201103L
55
56 /**
57 * @addtogroup utilities
58 * @{
59 */
60
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 */
67 template<typename _Tp>
68 _GLIBCXX_NODISCARD
69 constexpr _Tp&&
70 forward(typename std::remove_reference<_Tp>::type& __t) noexcept
71 { return static_cast<_Tp&&>(__t); }
72
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 */
79 template<typename _Tp>
80 _GLIBCXX_NODISCARD
81 constexpr _Tp&&
82 forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
83 {
84 static_assert(!std::is_lvalue_reference<_Tp>::value,
85 "std::forward must not be used to convert an rvalue to an lvalue");
86 return static_cast<_Tp&&>(__t);
87 }
88
89 #if __glibcxx_forward_like // C++ >= 23
90 template<typename _Tp, typename _Up>
91 [[nodiscard]]
92 constexpr decltype(auto)
93 forward_like(_Up&& __x) noexcept
94 {
95 constexpr bool __as_rval = is_rvalue_reference_v<_Tp&&>;
96
97 if constexpr (is_const_v<remove_reference_t<_Tp>>)
98 {
99 using _Up2 = remove_reference_t<_Up>;
100 if constexpr (__as_rval)
101 return static_cast<const _Up2&&>(__x);
102 else
103 return static_cast<const _Up2&>(__x);
104 }
105 else
106 {
107 if constexpr (__as_rval)
108 return static_cast<remove_reference_t<_Up>&&>(__x);
109 else
110 return static_cast<_Up&>(__x);
111 }
112 }
113 #endif
114
115 /**
116 * @brief Convert a value to an rvalue.
117 * @param __t A thing of arbitrary type.
118 * @return The parameter cast to an rvalue-reference to allow moving it.
119 */
120 template<typename _Tp>
121 _GLIBCXX_NODISCARD
122 constexpr typename std::remove_reference<_Tp>::type&&
123 move(_Tp&& __t) noexcept
124 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
125
126
127 template<typename _Tp>
128 struct __move_if_noexcept_cond
129 : public __and_<__not_<is_nothrow_move_constructible<_Tp>>,
130 is_copy_constructible<_Tp>>::type { };
131
132 /**
133 * @brief Conditionally convert a value to an rvalue.
134 * @param __x A thing of arbitrary type.
135 * @return The parameter, possibly cast to an rvalue-reference.
136 *
137 * Same as std::move unless the type's move constructor could throw and the
138 * type is copyable, in which case an lvalue-reference is returned instead.
139 */
140 template<typename _Tp>
141 _GLIBCXX_NODISCARD
142 constexpr
143 __conditional_t<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>
144 move_if_noexcept(_Tp& __x) noexcept
145 { return std::move(__x); }
146
147 // declval, from type_traits.
148
149 /**
150 * @brief Returns the actual address of the object or function
151 * referenced by r, even in the presence of an overloaded
152 * operator&.
153 * @param __r Reference to an object or function.
154 * @return The actual address.
155 */
156 template<typename _Tp>
157 _GLIBCXX_NODISCARD
158 inline _GLIBCXX17_CONSTEXPR _Tp*
159 addressof(_Tp& __r) noexcept
160 { return std::__addressof(__r); }
161
162 // _GLIBCXX_RESOLVE_LIB_DEFECTS
163 // 2598. addressof works on temporaries
164 template<typename _Tp>
165 const _Tp* addressof(const _Tp&&) = delete;
166
167 // C++11 version of std::exchange for internal use.
168 template <typename _Tp, typename _Up = _Tp>
169 _GLIBCXX20_CONSTEXPR
170 inline _Tp
171 __exchange(_Tp& __obj, _Up&& __new_val)
172 {
173 _Tp __old_val = std::move(__obj);
174 __obj = std::forward<_Up>(__new_val);
175 return __old_val;
176 }
177
178 /// @} group utilities
179
180 #define _GLIBCXX_FWDREF(_Tp) _Tp&&
181 #define _GLIBCXX_MOVE(__val) std::move(__val)
182 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
183 #else
184 #define _GLIBCXX_FWDREF(_Tp) const _Tp&
185 #define _GLIBCXX_MOVE(__val) (__val)
186 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
187 #endif
188
189 /**
190 * @addtogroup utilities
191 * @{
192 */
193
194 /**
195 * @brief Swaps two values.
196 * @param __a A thing of arbitrary type.
197 * @param __b Another thing of arbitrary type.
198 * @return Nothing.
199 */
200 template<typename _Tp>
201 _GLIBCXX20_CONSTEXPR
202 inline
203 #if __cplusplus >= 201103L
204 typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>,
205 is_move_constructible<_Tp>,
206 is_move_assignable<_Tp>>::value>::type
207 #else
208 void
209 #endif
210 swap(_Tp& __a, _Tp& __b)
211 _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>,
212 is_nothrow_move_assignable<_Tp>>::value)
213 {
214 #if __cplusplus < 201103L
215 // concept requirements
216 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
217 #endif
218 _Tp __tmp = _GLIBCXX_MOVE(__a);
219 __a = _GLIBCXX_MOVE(__b);
220 __b = _GLIBCXX_MOVE(__tmp);
221 }
222
223 // _GLIBCXX_RESOLVE_LIB_DEFECTS
224 // DR 809. std::swap should be overloaded for array types.
225 /// Swap the contents of two arrays.
226 template<typename _Tp, size_t _Nm>
227 _GLIBCXX20_CONSTEXPR
228 inline
229 #if __cplusplus >= 201103L
230 typename enable_if<__is_swappable<_Tp>::value>::type
231 #else
232 void
233 #endif
234 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
235 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value)
236 {
237 for (size_t __n = 0; __n < _Nm; ++__n)
238 swap(__a[__n], __b[__n]);
239 }
240
241 /// @} group utilities
242 _GLIBCXX_END_NAMESPACE_VERSION
243 } // namespace
244
245 #endif /* _MOVE_H */