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 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   template<typename _Tp>
00042     inline _Tp*
00043     __addressof(_Tp& __r)
00044     {
00045       return reinterpret_cast<_Tp*>
00046     (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r)));
00047     }
00048 
00049 _GLIBCXX_END_NAMESPACE_VERSION
00050 } // namespace
00051 
00052 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00053 #include <type_traits> // Brings in std::declval too.
00054 
00055 namespace std _GLIBCXX_VISIBILITY(default)
00056 {
00057 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00058   
00059   /// forward (as per N3143)
00060   template<typename _Tp>
00061     inline _Tp&&
00062     forward(typename std::remove_reference<_Tp>::type& __t) 
00063     { return static_cast<_Tp&&>(__t); }
00064 
00065   template<typename _Tp>
00066     inline _Tp&&
00067     forward(typename std::remove_reference<_Tp>::type&& __t) 
00068     {
00069       static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument"
00070             " substituting _Tp is an lvalue reference type");
00071       return static_cast<_Tp&&>(__t);
00072     }
00073 
00074   /**
00075    *  @brief Move a value.
00076    *  @ingroup mutating_algorithms
00077    *  @param  __t  A thing of arbitrary type.
00078    *  @return Same, moved.
00079   */
00080   template<typename _Tp>
00081     inline typename std::remove_reference<_Tp>::type&&
00082     move(_Tp&& __t)
00083     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
00084 
00085   /// declval, from type_traits.
00086 
00087   /**
00088    *  @brief Returns the actual address of the object or function
00089    *         referenced by r, even in the presence of an overloaded
00090    *         operator&.
00091    *  @param  __r  Reference to an object or function.
00092    *  @return   The actual address.
00093   */
00094   template<typename _Tp>
00095     inline _Tp*
00096     addressof(_Tp& __r)
00097     { return std::__addressof(__r); }
00098 
00099 _GLIBCXX_END_NAMESPACE_VERSION
00100 } // namespace
00101 
00102 #define _GLIBCXX_MOVE(__val) std::move(__val)
00103 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
00104 #else
00105 #define _GLIBCXX_MOVE(__val) (__val)
00106 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
00107 #endif
00108 
00109 namespace std _GLIBCXX_VISIBILITY(default)
00110 {
00111 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00112 
00113   /**
00114    *  @brief Swaps two values.
00115    *  @ingroup mutating_algorithms
00116    *  @param  __a  A thing of arbitrary type.
00117    *  @param  __b  Another thing of arbitrary type.
00118    *  @return   Nothing.
00119   */
00120   template<typename _Tp>
00121     inline void
00122     swap(_Tp& __a, _Tp& __b)
00123     {
00124       // concept requirements
00125       __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
00126 
00127       _Tp __tmp = _GLIBCXX_MOVE(__a);
00128       __a = _GLIBCXX_MOVE(__b);
00129       __b = _GLIBCXX_MOVE(__tmp);
00130     }
00131 
00132   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00133   // DR 809. std::swap should be overloaded for array types.
00134   template<typename _Tp, size_t _Nm>
00135     inline void
00136     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
00137     {
00138       for (size_t __n = 0; __n < _Nm; ++__n)
00139     swap(__a[__n], __b[__n]);
00140     }
00141 
00142 _GLIBCXX_END_NAMESPACE_VERSION
00143 } // namespace
00144 
00145 #endif /* _MOVE_H */