]>
Commit | Line | Data |
---|---|---|
1eb7eb4d | 1 | // Move, forward and identity for C++11 + swap -*- C++ -*- |
d5ed60d6 | 2 | |
fbd26352 | 3 | // Copyright (C) 2007-2019 Free Software Foundation, Inc. |
d5ed60d6 | 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 | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
d5ed60d6 | 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 | ||
6bc9506f | 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/>. | |
d5ed60d6 | 24 | |
5846aeac | 25 | /** @file bits/move.h |
d5ed60d6 | 26 | * This is an internal header file, included by other library headers. |
5846aeac | 27 | * Do not attempt to use it directly. @headername{utility} |
d5ed60d6 | 28 | */ |
29 | ||
40047e62 | 30 | #ifndef _MOVE_H |
31 | #define _MOVE_H 1 | |
d5ed60d6 | 32 | |
093ffe23 | 33 | #include <bits/c++config.h> |
34 | #include <bits/concept_check.h> | |
35 | ||
2948dd21 | 36 | namespace std _GLIBCXX_VISIBILITY(default) |
37 | { | |
38 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
53af708b | 39 | |
40 | // Used, in C++03 mode too, by allocators, etc. | |
3598538c | 41 | /** |
42 | * @brief Same as C++11 std::addressof | |
43 | * @ingroup utilities | |
44 | */ | |
53af708b | 45 | template<typename _Tp> |
c3f9a179 | 46 | inline _GLIBCXX_CONSTEXPR _Tp* |
7643295e | 47 | __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT |
c3f9a179 | 48 | { return __builtin_addressof(__r); } |
53af708b | 49 | |
ae6a4ce9 | 50 | #if __cplusplus >= 201103L |
51 | ||
2948dd21 | 52 | _GLIBCXX_END_NAMESPACE_VERSION |
53 | } // namespace | |
53af708b | 54 | |
08ab8a87 | 55 | #include <type_traits> // Brings in std::declval too. |
d5ed60d6 | 56 | |
2948dd21 | 57 | namespace std _GLIBCXX_VISIBILITY(default) |
58 | { | |
59 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3598538c | 60 | |
61 | /** | |
62 | * @addtogroup utilities | |
63 | * @{ | |
64 | */ | |
65 | ||
3598538c | 66 | /** |
67 | * @brief Forward an lvalue. | |
68 | * @return The parameter cast to the specified type. | |
69 | * | |
70 | * This function is used to implement "perfect forwarding". | |
71 | */ | |
74efe39a | 72 | template<typename _Tp> |
d07bb4f7 | 73 | constexpr _Tp&& |
7643295e | 74 | forward(typename std::remove_reference<_Tp>::type& __t) noexcept |
74efe39a | 75 | { return static_cast<_Tp&&>(__t); } |
76 | ||
3598538c | 77 | /** |
78 | * @brief Forward an rvalue. | |
79 | * @return The parameter cast to the specified type. | |
80 | * | |
81 | * This function is used to implement "perfect forwarding". | |
82 | */ | |
74efe39a | 83 | template<typename _Tp> |
d07bb4f7 | 84 | constexpr _Tp&& |
7643295e | 85 | forward(typename std::remove_reference<_Tp>::type&& __t) noexcept |
7719751d | 86 | { |
87 | static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument" | |
88 | " substituting _Tp is an lvalue reference type"); | |
89 | return static_cast<_Tp&&>(__t); | |
90 | } | |
aaa6e1c2 | 91 | |
530eec58 | 92 | /** |
3598538c | 93 | * @brief Convert a value to an rvalue. |
530eec58 | 94 | * @param __t A thing of arbitrary type. |
3598538c | 95 | * @return The parameter cast to an rvalue-reference to allow moving it. |
530eec58 | 96 | */ |
d5ed60d6 | 97 | template<typename _Tp> |
d07bb4f7 | 98 | constexpr typename std::remove_reference<_Tp>::type&& |
7643295e | 99 | move(_Tp&& __t) noexcept |
aaa6e1c2 | 100 | { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } |
d5ed60d6 | 101 | |
b74d2ad5 | 102 | |
103 | template<typename _Tp> | |
104 | struct __move_if_noexcept_cond | |
105 | : public __and_<__not_<is_nothrow_move_constructible<_Tp>>, | |
106 | is_copy_constructible<_Tp>>::type { }; | |
107 | ||
82ba6754 | 108 | /** |
3598538c | 109 | * @brief Conditionally convert a value to an rvalue. |
82ba6754 | 110 | * @param __x A thing of arbitrary type. |
3598538c | 111 | * @return The parameter, possibly cast to an rvalue-reference. |
112 | * | |
113 | * Same as std::move unless the type's move constructor could throw and the | |
114 | * type is copyable, in which case an lvalue-reference is returned instead. | |
82ba6754 | 115 | */ |
116 | template<typename _Tp> | |
06b72416 | 117 | constexpr typename |
b74d2ad5 | 118 | conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type |
82ba6754 | 119 | move_if_noexcept(_Tp& __x) noexcept |
120 | { return std::move(__x); } | |
121 | ||
3598538c | 122 | // declval, from type_traits. |
08ab8a87 | 123 | |
c3f9a179 | 124 | #if __cplusplus > 201402L |
125 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
126 | // 2296. std::addressof should be constexpr | |
127 | # define __cpp_lib_addressof_constexpr 201603 | |
128 | #endif | |
53af708b | 129 | /** |
130 | * @brief Returns the actual address of the object or function | |
131 | * referenced by r, even in the presence of an overloaded | |
132 | * operator&. | |
133 | * @param __r Reference to an object or function. | |
134 | * @return The actual address. | |
135 | */ | |
136 | template<typename _Tp> | |
c3f9a179 | 137 | inline _GLIBCXX17_CONSTEXPR _Tp* |
7643295e | 138 | addressof(_Tp& __r) noexcept |
53af708b | 139 | { return std::__addressof(__r); } |
140 | ||
05c6fa50 | 141 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
142 | // 2598. addressof works on temporaries | |
143 | template<typename _Tp> | |
144 | const _Tp* addressof(const _Tp&&) = delete; | |
145 | ||
28f8cba2 | 146 | // C++11 version of std::exchange for internal use. |
147 | template <typename _Tp, typename _Up = _Tp> | |
148 | inline _Tp | |
149 | __exchange(_Tp& __obj, _Up&& __new_val) | |
150 | { | |
151 | _Tp __old_val = std::move(__obj); | |
152 | __obj = std::forward<_Up>(__new_val); | |
153 | return __old_val; | |
154 | } | |
155 | ||
3598538c | 156 | /// @} group utilities |
d5ed60d6 | 157 | |
d01a2eb2 | 158 | #define _GLIBCXX_MOVE(__val) std::move(__val) |
58a61ad5 | 159 | #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) |
d42ae08a | 160 | #else |
d01a2eb2 | 161 | #define _GLIBCXX_MOVE(__val) (__val) |
58a61ad5 | 162 | #define _GLIBCXX_FORWARD(_Tp, __val) (__val) |
d5ed60d6 | 163 | #endif |
164 | ||
3598538c | 165 | /** |
166 | * @addtogroup utilities | |
167 | * @{ | |
168 | */ | |
169 | ||
093ffe23 | 170 | /** |
171 | * @brief Swaps two values. | |
530eec58 | 172 | * @param __a A thing of arbitrary type. |
173 | * @param __b Another thing of arbitrary type. | |
093ffe23 | 174 | * @return Nothing. |
175 | */ | |
176 | template<typename _Tp> | |
b5773487 | 177 | inline |
0c8766b1 | 178 | #if __cplusplus >= 201103L |
f89b61dd | 179 | typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>, |
180 | is_move_constructible<_Tp>, | |
b5773487 | 181 | is_move_assignable<_Tp>>::value>::type |
182 | swap(_Tp& __a, _Tp& __b) | |
c95bf15b | 183 | noexcept(__and_<is_nothrow_move_constructible<_Tp>, |
184 | is_nothrow_move_assignable<_Tp>>::value) | |
b5773487 | 185 | #else |
186 | void | |
187 | swap(_Tp& __a, _Tp& __b) | |
68757038 | 188 | #endif |
093ffe23 | 189 | { |
190 | // concept requirements | |
191 | __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) | |
192 | ||
193 | _Tp __tmp = _GLIBCXX_MOVE(__a); | |
194 | __a = _GLIBCXX_MOVE(__b); | |
195 | __b = _GLIBCXX_MOVE(__tmp); | |
196 | } | |
197 | ||
40047e62 | 198 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
199 | // DR 809. std::swap should be overloaded for array types. | |
3598538c | 200 | /// Swap the contents of two arrays. |
40047e62 | 201 | template<typename _Tp, size_t _Nm> |
b5773487 | 202 | inline |
0c8766b1 | 203 | #if __cplusplus >= 201103L |
03ff0289 | 204 | typename enable_if<__is_swappable<_Tp>::value>::type |
b5773487 | 205 | swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) |
03ff0289 | 206 | noexcept(__is_nothrow_swappable<_Tp>::value) |
b5773487 | 207 | #else |
208 | void | |
209 | swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) | |
5e44cded | 210 | #endif |
40047e62 | 211 | { |
212 | for (size_t __n = 0; __n < _Nm; ++__n) | |
213 | swap(__a[__n], __b[__n]); | |
214 | } | |
215 | ||
3598538c | 216 | /// @} group utilities |
2948dd21 | 217 | _GLIBCXX_END_NAMESPACE_VERSION |
218 | } // namespace | |
093ffe23 | 219 | |
40047e62 | 220 | #endif /* _MOVE_H */ |