1// Move, forward and identity for C++11 + swap -*- C++ -*-
2
3// Copyright (C) 2007-2024 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
40namespace 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 * @since C++11
67 */
68 template<typename _Tp>
69 _GLIBCXX_NODISCARD
70 constexpr _Tp&&
71 forward(typename std::remove_reference<_Tp>::type& __t) noexcept
72 { return static_cast<_Tp&&>(__t); }
73
74 /**
75 * @brief Forward an rvalue.
76 * @return The parameter cast to the specified type.
77 *
78 * This function is used to implement "perfect forwarding".
79 * @since C++11
80 */
81 template<typename _Tp>
82 _GLIBCXX_NODISCARD
83 constexpr _Tp&&
84 forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
85 {
86 static_assert(!std::is_lvalue_reference<_Tp>::value,
87 "std::forward must not be used to convert an rvalue to an lvalue");
88 return static_cast<_Tp&&>(__t);
89 }
90
91#if __glibcxx_forward_like // C++ >= 23
92 template<typename _Tp, typename _Up>
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&&; };
110
111 template<typename _Tp, typename _Up>
112 using __like_t = typename __like_impl<_Tp&&, _Up&>::type;
113
114 /** @brief Forward with the cv-qualifiers and value category of another type.
115 * @tparam _Tp An lvalue reference or rvalue reference.
116 * @tparam _Up An lvalue reference type deduced from the function argument.
117 * @param __x An lvalue.
118 * @return `__x` converted to match the qualifiers of `_Tp`.
119 * @since C++23
120 */
121 template<typename _Tp, typename _Up>
122 [[nodiscard]]
123 constexpr __like_t<_Tp, _Up>
124 forward_like(_Up&& __x) noexcept
125 { return static_cast<__like_t<_Tp, _Up>>(__x); }
126#endif
127
128 /**
129 * @brief Convert a value to an rvalue.
130 * @param __t A thing of arbitrary type.
131 * @return The parameter cast to an rvalue-reference to allow moving it.
132 * @since C++11
133 */
134 template<typename _Tp>
135 _GLIBCXX_NODISCARD
136 constexpr typename std::remove_reference<_Tp>::type&&
137 move(_Tp&& __t) noexcept
138 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
139
140
141 template<typename _Tp>
142 struct __move_if_noexcept_cond
143 : public __and_<__not_<is_nothrow_move_constructible<_Tp>>,
144 is_copy_constructible<_Tp>>::type { };
145
146 /**
147 * @brief Conditionally convert a value to an rvalue.
148 * @param __x A thing of arbitrary type.
149 * @return The parameter, possibly cast to an rvalue-reference.
150 *
151 * Same as std::move unless the type's move constructor could throw and the
152 * type is copyable, in which case an lvalue-reference is returned instead.
153 * @since C++11
154 */
155 template<typename _Tp>
156 _GLIBCXX_NODISCARD
157 constexpr
158 __conditional_t<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>
159 move_if_noexcept(_Tp& __x) noexcept
160 { return std::move(__x); }
161
162 // declval, from type_traits.
163
164 /**
165 * @brief Returns the actual address of the object or function
166 * referenced by r, even in the presence of an overloaded
167 * operator&.
168 * @param __r Reference to an object or function.
169 * @return The actual address.
170 * @since C++11
171 */
172 template<typename _Tp>
173 _GLIBCXX_NODISCARD
174 inline _GLIBCXX17_CONSTEXPR _Tp*
175 addressof(_Tp& __r) noexcept
176 { return std::__addressof(__r); }
177
178 // _GLIBCXX_RESOLVE_LIB_DEFECTS
179 // 2598. addressof works on temporaries
180 template<typename _Tp>
181 const _Tp* addressof(const _Tp&&) = delete;
182
183 // C++11 version of std::exchange for internal use.
184 template <typename _Tp, typename _Up = _Tp>
185 _GLIBCXX20_CONSTEXPR
186 inline _Tp
187 __exchange(_Tp& __obj, _Up&& __new_val)
188 {
189 _Tp __old_val = std::move(__obj);
190 __obj = std::forward<_Up>(__new_val);
191 return __old_val;
192 }
193
194 /// @} group utilities
195
196#define _GLIBCXX_FWDREF(_Tp) _Tp&&
197#define _GLIBCXX_MOVE(__val) std::move(__val)
198#define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
199#else
200#define _GLIBCXX_FWDREF(_Tp) const _Tp&
201#define _GLIBCXX_MOVE(__val) (__val)
202#define _GLIBCXX_FORWARD(_Tp, __val) (__val)
203#endif
204
205 /**
206 * @addtogroup utilities
207 * @{
208 */
209
210 /**
211 * @brief Swaps two values.
212 * @param __a A thing of arbitrary type.
213 * @param __b Another thing of arbitrary type.
214 * @return Nothing.
215 */
216 template<typename _Tp>
217 _GLIBCXX20_CONSTEXPR
218 inline
219#if __cplusplus >= 201103L
220 typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>,
221 is_move_constructible<_Tp>,
222 is_move_assignable<_Tp>>::value>::type
223#else
224 void
225#endif
226 swap(_Tp& __a, _Tp& __b)
227 _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>,
228 is_nothrow_move_assignable<_Tp>>::value)
229 {
230#if __cplusplus < 201103L
231 // concept requirements
232 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
233#endif
234 _Tp __tmp = _GLIBCXX_MOVE(__a);
235 __a = _GLIBCXX_MOVE(__b);
236 __b = _GLIBCXX_MOVE(__tmp);
237 }
238
239 // _GLIBCXX_RESOLVE_LIB_DEFECTS
240 // DR 809. std::swap should be overloaded for array types.
241 /// Swap the contents of two arrays.
242 template<typename _Tp, size_t _Nm>
243 _GLIBCXX20_CONSTEXPR
244 inline
245#if __cplusplus >= 201103L
246 typename enable_if<__is_swappable<_Tp>::value>::type
247#else
248 void
249#endif
250 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
251 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value)
252 {
253 for (size_t __n = 0; __n < _Nm; ++__n)
254 swap(__a[__n], __b[__n]);
255 }
256
257 /// @} group utilities
258_GLIBCXX_END_NAMESPACE_VERSION
259} // namespace
260
261#endif /* _MOVE_H */
262