libstdc++
|
00001 // Move, forward and identity for C++0x + swap -*- C++ -*- 00002 00003 // Copyright (C) 2007-2013 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 _Tp* 00047 __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT 00048 { 00049 return reinterpret_cast<_Tp*> 00050 (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r))); 00051 } 00052 00053 _GLIBCXX_END_NAMESPACE_VERSION 00054 } // namespace 00055 00056 #if __cplusplus >= 201103L 00057 #include <type_traits> // Brings in std::declval too. 00058 00059 namespace std _GLIBCXX_VISIBILITY(default) 00060 { 00061 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00062 00063 /** 00064 * @addtogroup utilities 00065 * @{ 00066 */ 00067 00068 /** 00069 * @brief Forward an lvalue. 00070 * @return The parameter cast to the specified type. 00071 * 00072 * This function is used to implement "perfect forwarding". 00073 */ 00074 template<typename _Tp> 00075 constexpr _Tp&& 00076 forward(typename std::remove_reference<_Tp>::type& __t) noexcept 00077 { return static_cast<_Tp&&>(__t); } 00078 00079 /** 00080 * @brief Forward an rvalue. 00081 * @return The parameter cast to the specified type. 00082 * 00083 * This function is used to implement "perfect forwarding". 00084 */ 00085 template<typename _Tp> 00086 constexpr _Tp&& 00087 forward(typename std::remove_reference<_Tp>::type&& __t) noexcept 00088 { 00089 static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument" 00090 " substituting _Tp is an lvalue reference type"); 00091 return static_cast<_Tp&&>(__t); 00092 } 00093 00094 /** 00095 * @brief Convert a value to an rvalue. 00096 * @param __t A thing of arbitrary type. 00097 * @return The parameter cast to an rvalue-reference to allow moving it. 00098 */ 00099 template<typename _Tp> 00100 constexpr typename std::remove_reference<_Tp>::type&& 00101 move(_Tp&& __t) noexcept 00102 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } 00103 00104 00105 template<typename _Tp> 00106 struct __move_if_noexcept_cond 00107 : public __and_<__not_<is_nothrow_move_constructible<_Tp>>, 00108 is_copy_constructible<_Tp>>::type { }; 00109 00110 /** 00111 * @brief Conditionally convert a value to an rvalue. 00112 * @param __x A thing of arbitrary type. 00113 * @return The parameter, possibly cast to an rvalue-reference. 00114 * 00115 * Same as std::move unless the type's move constructor could throw and the 00116 * type is copyable, in which case an lvalue-reference is returned instead. 00117 */ 00118 template<typename _Tp> 00119 inline constexpr typename 00120 conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type 00121 move_if_noexcept(_Tp& __x) noexcept 00122 { return std::move(__x); } 00123 00124 // declval, from type_traits. 00125 00126 /** 00127 * @brief Returns the actual address of the object or function 00128 * referenced by r, even in the presence of an overloaded 00129 * operator&. 00130 * @param __r Reference to an object or function. 00131 * @return The actual address. 00132 */ 00133 template<typename _Tp> 00134 inline _Tp* 00135 addressof(_Tp& __r) noexcept 00136 { return std::__addressof(__r); } 00137 00138 /// @} group utilities 00139 _GLIBCXX_END_NAMESPACE_VERSION 00140 } // namespace 00141 00142 #define _GLIBCXX_MOVE(__val) std::move(__val) 00143 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) 00144 #else 00145 #define _GLIBCXX_MOVE(__val) (__val) 00146 #define _GLIBCXX_FORWARD(_Tp, __val) (__val) 00147 #endif 00148 00149 namespace std _GLIBCXX_VISIBILITY(default) 00150 { 00151 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00152 00153 /** 00154 * @addtogroup utilities 00155 * @{ 00156 */ 00157 00158 /** 00159 * @brief Swaps two values. 00160 * @param __a A thing of arbitrary type. 00161 * @param __b Another thing of arbitrary type. 00162 * @return Nothing. 00163 */ 00164 template<typename _Tp> 00165 inline void 00166 swap(_Tp& __a, _Tp& __b) 00167 #if __cplusplus >= 201103L 00168 noexcept(__and_<is_nothrow_move_constructible<_Tp>, 00169 is_nothrow_move_assignable<_Tp>>::value) 00170 #endif 00171 { 00172 // concept requirements 00173 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) 00174 00175 _Tp __tmp = _GLIBCXX_MOVE(__a); 00176 __a = _GLIBCXX_MOVE(__b); 00177 __b = _GLIBCXX_MOVE(__tmp); 00178 } 00179 00180 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00181 // DR 809. std::swap should be overloaded for array types. 00182 /// Swap the contents of two arrays. 00183 template<typename _Tp, size_t _Nm> 00184 inline void 00185 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) 00186 #if __cplusplus >= 201103L 00187 noexcept(noexcept(swap(*__a, *__b))) 00188 #endif 00189 { 00190 for (size_t __n = 0; __n < _Nm; ++__n) 00191 swap(__a[__n], __b[__n]); 00192 } 00193 00194 /// @} group utilities 00195 _GLIBCXX_END_NAMESPACE_VERSION 00196 } // namespace 00197 00198 #endif /* _MOVE_H */