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