1// Move, forward and identity for C++0x + swap -*- C++ -*- 2 3// Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. 4// 5// This file is part of the GNU ISO C++ Library. This library is free 6// software; you can redistribute it and/or modify it under the 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 3, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// Under Section 7 of GPL version 3, you are granted additional 17// permissions described in the GCC Runtime Library Exception, version 18// 3.1, as published by the Free Software Foundation. 19 20// You should have received a copy of the GNU General Public License and 21// a copy of the GCC Runtime Library Exception along with this program; 22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23// <http://www.gnu.org/licenses/>. 24 25/** @file move.h 26 * This is an internal header file, included by other library headers. 27 * You should not attempt to use it directly. 28 */ 29 30#ifndef _MOVE_H 31#define _MOVE_H 1 32 33#include <bits/c++config.h> 34#include <cstddef> 35#include <bits/concept_check.h> 36 37#ifdef __GXX_EXPERIMENTAL_CXX0X__ 38#include <type_traits> 39 40_GLIBCXX_BEGIN_NAMESPACE(std) 41 42 // 20.2.2, forward/move 43 template<typename _Tp> 44 struct identity 45 { 46 typedef _Tp type; 47 }; 48 49 template<typename _Tp> 50 inline _Tp&& 51 forward(typename std::remove_reference<_Tp>::type& __t) 52#ifdef __clang__ 53 { return static_cast<_Tp&&>(__t); } 54#else 55 { return __t; } 56#endif 57 58 template<typename _Tp> 59 inline _Tp&& 60 forward(typename std::remove_reference<_Tp>::type&& __t) 61 { 62#ifdef __clang__ 63 static_assert(!std::is_lvalue_reference<_Tp>::value, 64 "Can't instantiate this forward() with an" 65 " lvalue reference type."); 66 return static_cast<_Tp&&>(__t); 67#else 68 return __t; 69#endif 70 } 71 72 template<typename _Tp> 73 inline typename std::remove_reference<_Tp>::type&& 74 move(_Tp&& __t) 75#ifdef __clang__ 76 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } 77#else 78 { return __t; } 79#endif 80 81_GLIBCXX_END_NAMESPACE 82 83#define _GLIBCXX_MOVE(_Tp) std::move(_Tp) 84#else 85#define _GLIBCXX_MOVE(_Tp) (_Tp) 86#endif 87 88_GLIBCXX_BEGIN_NAMESPACE(std) 89 90 /** 91 * @brief Swaps two values. 92 * @param a A thing of arbitrary type. 93 * @param b Another thing of arbitrary type. 94 * @return Nothing. 95 */ 96 template<typename _Tp> 97 inline void 98 swap(_Tp& __a, _Tp& __b) 99 { 100 // concept requirements 101 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) 102 103 _Tp __tmp = _GLIBCXX_MOVE(__a); 104 __a = _GLIBCXX_MOVE(__b); 105 __b = _GLIBCXX_MOVE(__tmp); 106 } 107 108 // _GLIBCXX_RESOLVE_LIB_DEFECTS 109 // DR 809. std::swap should be overloaded for array types. 110 template<typename _Tp, size_t _Nm> 111 inline void 112 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) 113 { 114 for (size_t __n = 0; __n < _Nm; ++__n) 115 swap(__a[__n], __b[__n]); 116 } 117 118_GLIBCXX_END_NAMESPACE 119 120#endif /* _MOVE_H */ 121