move.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Move, forward and identity for C++11 + swap -*- C++ -*-
  2. // Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/move.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{utility}
  23. */
  24. #ifndef _MOVE_H
  25. #define _MOVE_H 1
  26. #include <bits/c++config.h>
  27. #if __cplusplus < 201103L
  28. # include <bits/concept_check.h>
  29. #endif
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. // Used, in C++03 mode too, by allocators, etc.
  34. /**
  35. * @brief Same as C++11 std::addressof
  36. * @ingroup utilities
  37. */
  38. template<typename _Tp>
  39. inline _GLIBCXX_CONSTEXPR _Tp*
  40. __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
  41. { return __builtin_addressof(__r); }
  42. #if __cplusplus >= 201103L
  43. _GLIBCXX_END_NAMESPACE_VERSION
  44. } // namespace
  45. #include <type_traits> // Brings in std::declval too.
  46. namespace std _GLIBCXX_VISIBILITY(default)
  47. {
  48. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  49. /**
  50. * @addtogroup utilities
  51. * @{
  52. */
  53. /**
  54. * @brief Forward an lvalue.
  55. * @return The parameter cast to the specified type.
  56. *
  57. * This function is used to implement "perfect forwarding".
  58. */
  59. template<typename _Tp>
  60. _GLIBCXX_NODISCARD
  61. constexpr _Tp&&
  62. forward(typename std::remove_reference<_Tp>::type& __t) noexcept
  63. { return static_cast<_Tp&&>(__t); }
  64. /**
  65. * @brief Forward an rvalue.
  66. * @return The parameter cast to the specified type.
  67. *
  68. * This function is used to implement "perfect forwarding".
  69. */
  70. template<typename _Tp>
  71. _GLIBCXX_NODISCARD
  72. constexpr _Tp&&
  73. forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
  74. {
  75. static_assert(!std::is_lvalue_reference<_Tp>::value,
  76. "std::forward must not be used to convert an rvalue to an lvalue");
  77. return static_cast<_Tp&&>(__t);
  78. }
  79. /**
  80. * @brief Convert a value to an rvalue.
  81. * @param __t A thing of arbitrary type.
  82. * @return The parameter cast to an rvalue-reference to allow moving it.
  83. */
  84. template<typename _Tp>
  85. _GLIBCXX_NODISCARD
  86. constexpr typename std::remove_reference<_Tp>::type&&
  87. move(_Tp&& __t) noexcept
  88. { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
  89. template<typename _Tp>
  90. struct __move_if_noexcept_cond
  91. : public __and_<__not_<is_nothrow_move_constructible<_Tp>>,
  92. is_copy_constructible<_Tp>>::type { };
  93. /**
  94. * @brief Conditionally convert a value to an rvalue.
  95. * @param __x A thing of arbitrary type.
  96. * @return The parameter, possibly cast to an rvalue-reference.
  97. *
  98. * Same as std::move unless the type's move constructor could throw and the
  99. * type is copyable, in which case an lvalue-reference is returned instead.
  100. */
  101. template<typename _Tp>
  102. _GLIBCXX_NODISCARD
  103. constexpr
  104. __conditional_t<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>
  105. move_if_noexcept(_Tp& __x) noexcept
  106. { return std::move(__x); }
  107. // declval, from type_traits.
  108. #if __cplusplus > 201402L
  109. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  110. // 2296. std::addressof should be constexpr
  111. # define __cpp_lib_addressof_constexpr 201603L
  112. #endif
  113. /**
  114. * @brief Returns the actual address of the object or function
  115. * referenced by r, even in the presence of an overloaded
  116. * operator&.
  117. * @param __r Reference to an object or function.
  118. * @return The actual address.
  119. */
  120. template<typename _Tp>
  121. _GLIBCXX_NODISCARD
  122. inline _GLIBCXX17_CONSTEXPR _Tp*
  123. addressof(_Tp& __r) noexcept
  124. { return std::__addressof(__r); }
  125. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  126. // 2598. addressof works on temporaries
  127. template<typename _Tp>
  128. const _Tp* addressof(const _Tp&&) = delete;
  129. // C++11 version of std::exchange for internal use.
  130. template <typename _Tp, typename _Up = _Tp>
  131. _GLIBCXX20_CONSTEXPR
  132. inline _Tp
  133. __exchange(_Tp& __obj, _Up&& __new_val)
  134. {
  135. _Tp __old_val = std::move(__obj);
  136. __obj = std::forward<_Up>(__new_val);
  137. return __old_val;
  138. }
  139. /// @} group utilities
  140. #define _GLIBCXX_FWDREF(_Tp) _Tp&&
  141. #define _GLIBCXX_MOVE(__val) std::move(__val)
  142. #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
  143. #else
  144. #define _GLIBCXX_FWDREF(_Tp) const _Tp&
  145. #define _GLIBCXX_MOVE(__val) (__val)
  146. #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
  147. #endif
  148. /**
  149. * @addtogroup utilities
  150. * @{
  151. */
  152. /**
  153. * @brief Swaps two values.
  154. * @param __a A thing of arbitrary type.
  155. * @param __b Another thing of arbitrary type.
  156. * @return Nothing.
  157. */
  158. template<typename _Tp>
  159. _GLIBCXX20_CONSTEXPR
  160. inline
  161. #if __cplusplus >= 201103L
  162. typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>,
  163. is_move_constructible<_Tp>,
  164. is_move_assignable<_Tp>>::value>::type
  165. #else
  166. void
  167. #endif
  168. swap(_Tp& __a, _Tp& __b)
  169. _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>,
  170. is_nothrow_move_assignable<_Tp>>::value)
  171. {
  172. #if __cplusplus < 201103L
  173. // concept requirements
  174. __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
  175. #endif
  176. _Tp __tmp = _GLIBCXX_MOVE(__a);
  177. __a = _GLIBCXX_MOVE(__b);
  178. __b = _GLIBCXX_MOVE(__tmp);
  179. }
  180. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  181. // DR 809. std::swap should be overloaded for array types.
  182. /// Swap the contents of two arrays.
  183. template<typename _Tp, size_t _Nm>
  184. _GLIBCXX20_CONSTEXPR
  185. inline
  186. #if __cplusplus >= 201103L
  187. typename enable_if<__is_swappable<_Tp>::value>::type
  188. #else
  189. void
  190. #endif
  191. swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
  192. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value)
  193. {
  194. for (size_t __n = 0; __n < _Nm; ++__n)
  195. swap(__a[__n], __b[__n]);
  196. }
  197. /// @} group utilities
  198. _GLIBCXX_END_NAMESPACE_VERSION
  199. } // namespace
  200. #endif /* _MOVE_H */