libstdc++
move.h
Go to the documentation of this file.
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  {
85  "std::forward must not be used to convert an rvalue to an lvalue");
86  return static_cast<_Tp&&>(__t);
87  }
88 
89  /**
90  * @brief Convert a value to an rvalue.
91  * @param __t A thing of arbitrary type.
92  * @return The parameter cast to an rvalue-reference to allow moving it.
93  */
94  template<typename _Tp>
95  _GLIBCXX_NODISCARD
96  constexpr typename std::remove_reference<_Tp>::type&&
97  move(_Tp&& __t) noexcept
98  { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
99 
100 
101  template<typename _Tp>
102  struct __move_if_noexcept_cond
103  : public __and_<__not_<is_nothrow_move_constructible<_Tp>>,
104  is_copy_constructible<_Tp>>::type { };
105 
106  /**
107  * @brief Conditionally convert a value to an rvalue.
108  * @param __x A thing of arbitrary type.
109  * @return The parameter, possibly cast to an rvalue-reference.
110  *
111  * Same as std::move unless the type's move constructor could throw and the
112  * type is copyable, in which case an lvalue-reference is returned instead.
113  */
114  template<typename _Tp>
115  _GLIBCXX_NODISCARD
116  constexpr
117  __conditional_t<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>
118  move_if_noexcept(_Tp& __x) noexcept
119  { return std::move(__x); }
120 
121  // declval, from type_traits.
122 
123 #if __cplusplus > 201402L
124  // _GLIBCXX_RESOLVE_LIB_DEFECTS
125  // 2296. std::addressof should be constexpr
126 # define __cpp_lib_addressof_constexpr 201603L
127 #endif
128  /**
129  * @brief Returns the actual address of the object or function
130  * referenced by r, even in the presence of an overloaded
131  * operator&.
132  * @param __r Reference to an object or function.
133  * @return The actual address.
134  */
135  template<typename _Tp>
136  _GLIBCXX_NODISCARD
137  inline _GLIBCXX17_CONSTEXPR _Tp*
138  addressof(_Tp& __r) noexcept
139  { return std::__addressof(__r); }
140 
141  // _GLIBCXX_RESOLVE_LIB_DEFECTS
142  // 2598. addressof works on temporaries
143  template<typename _Tp>
144  const _Tp* addressof(const _Tp&&) = delete;
145 
146  // C++11 version of std::exchange for internal use.
147  template <typename _Tp, typename _Up = _Tp>
148  _GLIBCXX20_CONSTEXPR
149  inline _Tp
150  __exchange(_Tp& __obj, _Up&& __new_val)
151  {
152  _Tp __old_val = std::move(__obj);
153  __obj = std::forward<_Up>(__new_val);
154  return __old_val;
155  }
156 
157  /// @} group utilities
158 
159 #define _GLIBCXX_FWDREF(_Tp) _Tp&&
160 #define _GLIBCXX_MOVE(__val) std::move(__val)
161 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
162 #else
163 #define _GLIBCXX_FWDREF(_Tp) const _Tp&
164 #define _GLIBCXX_MOVE(__val) (__val)
165 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
166 #endif
167 
168  /**
169  * @addtogroup utilities
170  * @{
171  */
172 
173  /**
174  * @brief Swaps two values.
175  * @param __a A thing of arbitrary type.
176  * @param __b Another thing of arbitrary type.
177  * @return Nothing.
178  */
179  template<typename _Tp>
180  _GLIBCXX20_CONSTEXPR
181  inline
182 #if __cplusplus >= 201103L
183  typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>,
184  is_move_constructible<_Tp>,
185  is_move_assignable<_Tp>>::value>::type
186 #else
187  void
188 #endif
189  swap(_Tp& __a, _Tp& __b)
190  _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>,
192  {
193 #if __cplusplus < 201103L
194  // concept requirements
195  __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
196 #endif
197  _Tp __tmp = _GLIBCXX_MOVE(__a);
198  __a = _GLIBCXX_MOVE(__b);
199  __b = _GLIBCXX_MOVE(__tmp);
200  }
201 
202  // _GLIBCXX_RESOLVE_LIB_DEFECTS
203  // DR 809. std::swap should be overloaded for array types.
204  /// Swap the contents of two arrays.
205  template<typename _Tp, size_t _Nm>
206  _GLIBCXX20_CONSTEXPR
207  inline
208 #if __cplusplus >= 201103L
209  typename enable_if<__is_swappable<_Tp>::value>::type
210 #else
211  void
212 #endif
213  swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
214  _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value)
215  {
216  for (size_t __n = 0; __n < _Nm; ++__n)
217  swap(__a[__n], __b[__n]);
218  }
219 
220  /// @} group utilities
221 _GLIBCXX_END_NAMESPACE_VERSION
222 } // namespace
223 
224 #endif /* _MOVE_H */
constexpr __conditional_t< __move_if_noexcept_cond< _Tp >::value, const _Tp &, _Tp && > move_if_noexcept(_Tp &__x) noexcept
Conditionally convert a value to an rvalue.
Definition: move.h:118
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:51
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:97
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:70
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:429
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition: move.h:138
ISO C++ entities toplevel namespace is std.
is_lvalue_reference
Definition: type_traits:535
is_nothrow_move_constructible
Definition: type_traits:1142
is_nothrow_move_assignable
Definition: type_traits:1210