libstdc++
|
00001 // Move, forward and identity for C++11 + swap -*- C++ -*- 00002 00003 // Copyright (C) 2007-2017 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/move.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{utility} 00028 */ 00029 00030 #ifndef _MOVE_H 00031 #define _MOVE_H 1 00032 00033 #include <bits/c++config.h> 00034 #include <bits/concept_check.h> 00035 00036 namespace std _GLIBCXX_VISIBILITY(default) 00037 { 00038 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00039 00040 // Used, in C++03 mode too, by allocators, etc. 00041 /** 00042 * @brief Same as C++11 std::addressof 00043 * @ingroup utilities 00044 */ 00045 template<typename _Tp> 00046 inline _GLIBCXX_CONSTEXPR _Tp* 00047 __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT 00048 { return __builtin_addressof(__r); } 00049 00050 _GLIBCXX_END_NAMESPACE_VERSION 00051 } // namespace 00052 00053 #if __cplusplus >= 201103L 00054 #include <type_traits> // Brings in std::declval too. 00055 00056 namespace std _GLIBCXX_VISIBILITY(default) 00057 { 00058 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00059 00060 /** 00061 * @addtogroup utilities 00062 * @{ 00063 */ 00064 00065 /** 00066 * @brief Forward an lvalue. 00067 * @return The parameter cast to the specified type. 00068 * 00069 * This function is used to implement "perfect forwarding". 00070 */ 00071 template<typename _Tp> 00072 constexpr _Tp&& 00073 forward(typename std::remove_reference<_Tp>::type& __t) noexcept 00074 { return static_cast<_Tp&&>(__t); } 00075 00076 /** 00077 * @brief Forward an rvalue. 00078 * @return The parameter cast to the specified type. 00079 * 00080 * This function is used to implement "perfect forwarding". 00081 */ 00082 template<typename _Tp> 00083 constexpr _Tp&& 00084 forward(typename std::remove_reference<_Tp>::type&& __t) noexcept 00085 { 00086 static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument" 00087 " substituting _Tp is an lvalue reference type"); 00088 return static_cast<_Tp&&>(__t); 00089 } 00090 00091 /** 00092 * @brief Convert a value to an rvalue. 00093 * @param __t A thing of arbitrary type. 00094 * @return The parameter cast to an rvalue-reference to allow moving it. 00095 */ 00096 template<typename _Tp> 00097 constexpr typename std::remove_reference<_Tp>::type&& 00098 move(_Tp&& __t) noexcept 00099 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } 00100 00101 00102 template<typename _Tp> 00103 struct __move_if_noexcept_cond 00104 : public __and_<__not_<is_nothrow_move_constructible<_Tp>>, 00105 is_copy_constructible<_Tp>>::type { }; 00106 00107 /** 00108 * @brief Conditionally convert a value to an rvalue. 00109 * @param __x A thing of arbitrary type. 00110 * @return The parameter, possibly cast to an rvalue-reference. 00111 * 00112 * Same as std::move unless the type's move constructor could throw and the 00113 * type is copyable, in which case an lvalue-reference is returned instead. 00114 */ 00115 template<typename _Tp> 00116 constexpr typename 00117 conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type 00118 move_if_noexcept(_Tp& __x) noexcept 00119 { return std::move(__x); } 00120 00121 // declval, from type_traits. 00122 00123 #if __cplusplus > 201402L 00124 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00125 // 2296. std::addressof should be constexpr 00126 # define __cpp_lib_addressof_constexpr 201603 00127 #endif 00128 /** 00129 * @brief Returns the actual address of the object or function 00130 * referenced by r, even in the presence of an overloaded 00131 * operator&. 00132 * @param __r Reference to an object or function. 00133 * @return The actual address. 00134 */ 00135 template<typename _Tp> 00136 inline _GLIBCXX17_CONSTEXPR _Tp* 00137 addressof(_Tp& __r) noexcept 00138 { return std::__addressof(__r); } 00139 00140 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00141 // 2598. addressof works on temporaries 00142 template<typename _Tp> 00143 const _Tp* addressof(const _Tp&&) = delete; 00144 00145 // C++11 version of std::exchange for internal use. 00146 template <typename _Tp, typename _Up = _Tp> 00147 inline _Tp 00148 __exchange(_Tp& __obj, _Up&& __new_val) 00149 { 00150 _Tp __old_val = std::move(__obj); 00151 __obj = std::forward<_Up>(__new_val); 00152 return __old_val; 00153 } 00154 00155 /// @} group utilities 00156 _GLIBCXX_END_NAMESPACE_VERSION 00157 } // namespace 00158 00159 #define _GLIBCXX_MOVE(__val) std::move(__val) 00160 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) 00161 #else 00162 #define _GLIBCXX_MOVE(__val) (__val) 00163 #define _GLIBCXX_FORWARD(_Tp, __val) (__val) 00164 #endif 00165 00166 namespace std _GLIBCXX_VISIBILITY(default) 00167 { 00168 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00169 00170 /** 00171 * @addtogroup utilities 00172 * @{ 00173 */ 00174 00175 /** 00176 * @brief Swaps two values. 00177 * @param __a A thing of arbitrary type. 00178 * @param __b Another thing of arbitrary type. 00179 * @return Nothing. 00180 */ 00181 template<typename _Tp> 00182 inline 00183 #if __cplusplus >= 201103L 00184 typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>, 00185 is_move_constructible<_Tp>, 00186 is_move_assignable<_Tp>>::value>::type 00187 swap(_Tp& __a, _Tp& __b) 00188 noexcept(__and_<is_nothrow_move_constructible<_Tp>, 00189 is_nothrow_move_assignable<_Tp>>::value) 00190 #else 00191 void 00192 swap(_Tp& __a, _Tp& __b) 00193 #endif 00194 { 00195 // concept requirements 00196 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) 00197 00198 _Tp __tmp = _GLIBCXX_MOVE(__a); 00199 __a = _GLIBCXX_MOVE(__b); 00200 __b = _GLIBCXX_MOVE(__tmp); 00201 } 00202 00203 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00204 // DR 809. std::swap should be overloaded for array types. 00205 /// Swap the contents of two arrays. 00206 template<typename _Tp, size_t _Nm> 00207 inline 00208 #if __cplusplus >= 201103L 00209 typename enable_if<__is_swappable<_Tp>::value>::type 00210 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) 00211 noexcept(__is_nothrow_swappable<_Tp>::value) 00212 #else 00213 void 00214 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) 00215 #endif 00216 { 00217 for (size_t __n = 0; __n < _Nm; ++__n) 00218 swap(__a[__n], __b[__n]); 00219 } 00220 00221 /// @} group utilities 00222 _GLIBCXX_END_NAMESPACE_VERSION 00223 } // namespace 00224 00225 #endif /* _MOVE_H */