move.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Move, forward and identity for C++0x + swap -*- C++ -*-
  2. // Copyright (C) 2007-2015 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. #include <bits/concept_check.h>
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. // Used, in C++03 mode too, by allocators, etc.
  32. /**
  33. * @brief Same as C++11 std::addressof
  34. * @ingroup utilities
  35. */
  36. template<typename _Tp>
  37. inline _Tp*
  38. __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
  39. {
  40. return reinterpret_cast<_Tp*>
  41. (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r)));
  42. }
  43. _GLIBCXX_END_NAMESPACE_VERSION
  44. } // namespace
  45. #if __cplusplus >= 201103L
  46. #include <type_traits> // Brings in std::declval too.
  47. namespace std _GLIBCXX_VISIBILITY(default)
  48. {
  49. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  50. /**
  51. * @addtogroup utilities
  52. * @{
  53. */
  54. /**
  55. * @brief Forward an lvalue.
  56. * @return The parameter cast to the specified type.
  57. *
  58. * This function is used to implement "perfect forwarding".
  59. */
  60. template<typename _Tp>
  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. constexpr _Tp&&
  72. forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
  73. {
  74. static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument"
  75. " substituting _Tp is an lvalue reference type");
  76. return static_cast<_Tp&&>(__t);
  77. }
  78. /**
  79. * @brief Convert a value to an rvalue.
  80. * @param __t A thing of arbitrary type.
  81. * @return The parameter cast to an rvalue-reference to allow moving it.
  82. */
  83. template<typename _Tp>
  84. constexpr typename std::remove_reference<_Tp>::type&&
  85. move(_Tp&& __t) noexcept
  86. { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
  87. template<typename _Tp>
  88. struct __move_if_noexcept_cond
  89. : public __and_<__not_<is_nothrow_move_constructible<_Tp>>,
  90. is_copy_constructible<_Tp>>::type { };
  91. /**
  92. * @brief Conditionally convert a value to an rvalue.
  93. * @param __x A thing of arbitrary type.
  94. * @return The parameter, possibly cast to an rvalue-reference.
  95. *
  96. * Same as std::move unless the type's move constructor could throw and the
  97. * type is copyable, in which case an lvalue-reference is returned instead.
  98. */
  99. template<typename _Tp>
  100. constexpr typename
  101. conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type
  102. move_if_noexcept(_Tp& __x) noexcept
  103. { return std::move(__x); }
  104. // declval, from type_traits.
  105. /**
  106. * @brief Returns the actual address of the object or function
  107. * referenced by r, even in the presence of an overloaded
  108. * operator&.
  109. * @param __r Reference to an object or function.
  110. * @return The actual address.
  111. */
  112. template<typename _Tp>
  113. inline _Tp*
  114. addressof(_Tp& __r) noexcept
  115. { return std::__addressof(__r); }
  116. // C++11 version of std::exchange for internal use.
  117. template <typename _Tp, typename _Up = _Tp>
  118. inline _Tp
  119. __exchange(_Tp& __obj, _Up&& __new_val)
  120. {
  121. _Tp __old_val = std::move(__obj);
  122. __obj = std::forward<_Up>(__new_val);
  123. return __old_val;
  124. }
  125. /// @} group utilities
  126. _GLIBCXX_END_NAMESPACE_VERSION
  127. } // namespace
  128. #define _GLIBCXX_MOVE(__val) std::move(__val)
  129. #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
  130. #else
  131. #define _GLIBCXX_MOVE(__val) (__val)
  132. #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
  133. #endif
  134. namespace std _GLIBCXX_VISIBILITY(default)
  135. {
  136. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  137. /**
  138. * @addtogroup utilities
  139. * @{
  140. */
  141. /**
  142. * @brief Swaps two values.
  143. * @param __a A thing of arbitrary type.
  144. * @param __b Another thing of arbitrary type.
  145. * @return Nothing.
  146. */
  147. template<typename _Tp>
  148. inline void
  149. swap(_Tp& __a, _Tp& __b)
  150. #if __cplusplus >= 201103L
  151. noexcept(__and_<is_nothrow_move_constructible<_Tp>,
  152. is_nothrow_move_assignable<_Tp>>::value)
  153. #endif
  154. {
  155. // concept requirements
  156. __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
  157. _Tp __tmp = _GLIBCXX_MOVE(__a);
  158. __a = _GLIBCXX_MOVE(__b);
  159. __b = _GLIBCXX_MOVE(__tmp);
  160. }
  161. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  162. // DR 809. std::swap should be overloaded for array types.
  163. /// Swap the contents of two arrays.
  164. template<typename _Tp, size_t _Nm>
  165. inline void
  166. swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
  167. #if __cplusplus >= 201103L
  168. noexcept(noexcept(swap(*__a, *__b)))
  169. #endif
  170. {
  171. for (size_t __n = 0; __n < _Nm; ++__n)
  172. swap(__a[__n], __b[__n]);
  173. }
  174. /// @} group utilities
  175. _GLIBCXX_END_NAMESPACE_VERSION
  176. } // namespace
  177. #endif /* _MOVE_H */