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