]>
Commit | Line | Data |
---|---|---|
e14e932b | 1 | // Move, forward and identity for C++0x + swap -*- C++ -*- |
1476e59c | 2 | |
a9ba8ba5 | 3 | // Copyright (C) 2007-2012 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 PC |
33 | #include <bits/c++config.h> |
34 | #include <bits/concept_check.h> | |
35 | ||
12ffa228 BK |
36 | namespace std _GLIBCXX_VISIBILITY(default) |
37 | { | |
38 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
882b3d5c PC |
39 | |
40 | // Used, in C++03 mode too, by allocators, etc. | |
13901e4b JW |
41 | /** |
42 | * @brief Same as C++11 std::addressof | |
43 | * @ingroup utilities | |
44 | */ | |
882b3d5c PC |
45 | template<typename _Tp> |
46 | inline _Tp* | |
5d861bf2 | 47 | __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT |
882b3d5c PC |
48 | { |
49 | return reinterpret_cast<_Tp*> | |
50 | (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r))); | |
51 | } | |
52 | ||
12ffa228 BK |
53 | _GLIBCXX_END_NAMESPACE_VERSION |
54 | } // namespace | |
882b3d5c | 55 | |
1476e59c | 56 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
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> |
a4eeb822 | 75 | constexpr _Tp&& |
5d861bf2 | 76 | forward(typename std::remove_reference<_Tp>::type& __t) noexcept |
4c7aaebf PC |
77 | { return static_cast<_Tp&&>(__t); } |
78 | ||
13901e4b JW |
79 | /** |
80 | * @brief Forward an rvalue. | |
81 | * @return The parameter cast to the specified type. | |
82 | * | |
83 | * This function is used to implement "perfect forwarding". | |
84 | */ | |
4c7aaebf | 85 | template<typename _Tp> |
a4eeb822 | 86 | constexpr _Tp&& |
5d861bf2 | 87 | forward(typename std::remove_reference<_Tp>::type&& __t) noexcept |
ad269884 PC |
88 | { |
89 | static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument" | |
90 | " substituting _Tp is an lvalue reference type"); | |
91 | return static_cast<_Tp&&>(__t); | |
92 | } | |
e7f1930f | 93 | |
fd9380a6 | 94 | /** |
13901e4b | 95 | * @brief Convert a value to an rvalue. |
fd9380a6 | 96 | * @param __t A thing of arbitrary type. |
13901e4b | 97 | * @return The parameter cast to an rvalue-reference to allow moving it. |
fd9380a6 | 98 | */ |
1476e59c | 99 | template<typename _Tp> |
a4eeb822 | 100 | constexpr typename std::remove_reference<_Tp>::type&& |
5d861bf2 | 101 | move(_Tp&& __t) noexcept |
e7f1930f | 102 | { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } |
1476e59c | 103 | |
74a2a1b4 PC |
104 | |
105 | template<typename _Tp> | |
106 | struct __move_if_noexcept_cond | |
107 | : public __and_<__not_<is_nothrow_move_constructible<_Tp>>, | |
108 | is_copy_constructible<_Tp>>::type { }; | |
109 | ||
1f428429 | 110 | /** |
13901e4b | 111 | * @brief Conditionally convert a value to an rvalue. |
1f428429 | 112 | * @param __x A thing of arbitrary type. |
13901e4b JW |
113 | * @return The parameter, possibly cast to an rvalue-reference. |
114 | * | |
115 | * Same as std::move unless the type's move constructor could throw and the | |
116 | * type is copyable, in which case an lvalue-reference is returned instead. | |
1f428429 PC |
117 | */ |
118 | template<typename _Tp> | |
a9ba8ba5 | 119 | inline constexpr typename |
74a2a1b4 | 120 | conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type |
1f428429 PC |
121 | move_if_noexcept(_Tp& __x) noexcept |
122 | { return std::move(__x); } | |
123 | ||
13901e4b | 124 | // declval, from type_traits. |
7274deff | 125 | |
882b3d5c PC |
126 | /** |
127 | * @brief Returns the actual address of the object or function | |
128 | * referenced by r, even in the presence of an overloaded | |
129 | * operator&. | |
130 | * @param __r Reference to an object or function. | |
131 | * @return The actual address. | |
132 | */ | |
133 | template<typename _Tp> | |
134 | inline _Tp* | |
5d861bf2 | 135 | addressof(_Tp& __r) noexcept |
882b3d5c PC |
136 | { return std::__addressof(__r); } |
137 | ||
13901e4b | 138 | /// @} group utilities |
12ffa228 BK |
139 | _GLIBCXX_END_NAMESPACE_VERSION |
140 | } // namespace | |
1476e59c | 141 | |
5f1fd346 | 142 | #define _GLIBCXX_MOVE(__val) std::move(__val) |
55dd8445 | 143 | #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) |
d98f312c | 144 | #else |
5f1fd346 | 145 | #define _GLIBCXX_MOVE(__val) (__val) |
55dd8445 | 146 | #define _GLIBCXX_FORWARD(_Tp, __val) (__val) |
1476e59c PC |
147 | #endif |
148 | ||
12ffa228 BK |
149 | namespace std _GLIBCXX_VISIBILITY(default) |
150 | { | |
151 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
e14e932b | 152 | |
13901e4b JW |
153 | /** |
154 | * @addtogroup utilities | |
155 | * @{ | |
156 | */ | |
157 | ||
e14e932b PC |
158 | /** |
159 | * @brief Swaps two values. | |
fd9380a6 BK |
160 | * @param __a A thing of arbitrary type. |
161 | * @param __b Another thing of arbitrary type. | |
e14e932b PC |
162 | * @return Nothing. |
163 | */ | |
164 | template<typename _Tp> | |
165 | inline void | |
166 | swap(_Tp& __a, _Tp& __b) | |
173f26ae | 167 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
6f59ea25 PC |
168 | noexcept(__and_<is_nothrow_move_constructible<_Tp>, |
169 | is_nothrow_move_assignable<_Tp>>::value) | |
173f26ae | 170 | #endif |
e14e932b PC |
171 | { |
172 | // concept requirements | |
173 | __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) | |
174 | ||
175 | _Tp __tmp = _GLIBCXX_MOVE(__a); | |
176 | __a = _GLIBCXX_MOVE(__b); | |
177 | __b = _GLIBCXX_MOVE(__tmp); | |
178 | } | |
179 | ||
caa8b3c6 PC |
180 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
181 | // DR 809. std::swap should be overloaded for array types. | |
13901e4b | 182 | /// Swap the contents of two arrays. |
caa8b3c6 PC |
183 | template<typename _Tp, size_t _Nm> |
184 | inline void | |
185 | swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) | |
ccb4f5a7 | 186 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
75f5cbb1 | 187 | noexcept(noexcept(swap(*__a, *__b))) |
ccb4f5a7 | 188 | #endif |
caa8b3c6 PC |
189 | { |
190 | for (size_t __n = 0; __n < _Nm; ++__n) | |
191 | swap(__a[__n], __b[__n]); | |
192 | } | |
193 | ||
13901e4b | 194 | /// @} group utilities |
12ffa228 BK |
195 | _GLIBCXX_END_NAMESPACE_VERSION |
196 | } // namespace | |
e14e932b | 197 | |
caa8b3c6 | 198 | #endif /* _MOVE_H */ |