basic_string.h: Use noexcept per the FDIS (minus compare(const string&)...
[gcc.git] / libstdc++-v3 / include / bits / move.h
1 // Move, forward and identity for C++0x + swap -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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 bits/move.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{utility}
28 */
29
30 #ifndef _MOVE_H
31 #define _MOVE_H 1
32
33 #include <bits/c++config.h>
34 #include <bits/concept_check.h>
35
36 namespace std _GLIBCXX_VISIBILITY(default)
37 {
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39
40 // Used, in C++03 mode too, by allocators, etc.
41 template<typename _Tp>
42 inline _Tp*
43 __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
44 {
45 return reinterpret_cast<_Tp*>
46 (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r)));
47 }
48
49 _GLIBCXX_END_NAMESPACE_VERSION
50 } // namespace
51
52 #ifdef __GXX_EXPERIMENTAL_CXX0X__
53 #include <type_traits> // Brings in std::declval too.
54
55 namespace std _GLIBCXX_VISIBILITY(default)
56 {
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58
59 /// forward (as per N3143)
60 template<typename _Tp>
61 inline _Tp&&
62 forward(typename std::remove_reference<_Tp>::type& __t) noexcept
63 { return static_cast<_Tp&&>(__t); }
64
65 template<typename _Tp>
66 inline _Tp&&
67 forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
68 {
69 static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument"
70 " substituting _Tp is an lvalue reference type");
71 return static_cast<_Tp&&>(__t);
72 }
73
74 /**
75 * @brief Move a value.
76 * @ingroup utilities
77 * @param __t A thing of arbitrary type.
78 * @return Same, moved.
79 */
80 template<typename _Tp>
81 inline typename std::remove_reference<_Tp>::type&&
82 move(_Tp&& __t) noexcept
83 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
84
85 /**
86 * @brief Move unless it could throw and the type is copyable.
87 * @ingroup utilities
88 * @param __x A thing of arbitrary type.
89 * @return Same, possibly moved.
90 */
91 template<typename _Tp>
92 inline typename
93 conditional<(!is_nothrow_move_constructible<_Tp>::value
94 && is_copy_constructible<_Tp>::value),
95 const _Tp&, _Tp&&>::type
96 move_if_noexcept(_Tp& __x) noexcept
97 { return std::move(__x); }
98
99 /// declval, from type_traits.
100
101 /**
102 * @brief Returns the actual address of the object or function
103 * referenced by r, even in the presence of an overloaded
104 * operator&.
105 * @ingroup utilities
106 * @param __r Reference to an object or function.
107 * @return The actual address.
108 */
109 template<typename _Tp>
110 inline _Tp*
111 addressof(_Tp& __r) noexcept
112 { return std::__addressof(__r); }
113
114 _GLIBCXX_END_NAMESPACE_VERSION
115 } // namespace
116
117 #define _GLIBCXX_MOVE(__val) std::move(__val)
118 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
119 #else
120 #define _GLIBCXX_MOVE(__val) (__val)
121 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
122 #endif
123
124 namespace std _GLIBCXX_VISIBILITY(default)
125 {
126 _GLIBCXX_BEGIN_NAMESPACE_VERSION
127
128 /**
129 * @brief Swaps two values.
130 * @ingroup utilities
131 * @param __a A thing of arbitrary type.
132 * @param __b Another thing of arbitrary type.
133 * @return Nothing.
134 */
135 template<typename _Tp>
136 inline void
137 swap(_Tp& __a, _Tp& __b)
138 #ifdef __GXX_EXPERIMENTAL_CXX0X__
139 noexcept(is_nothrow_move_constructible<_Tp>::value
140 && is_nothrow_move_assignable<_Tp>::value)
141 #endif
142 {
143 // concept requirements
144 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
145
146 _Tp __tmp = _GLIBCXX_MOVE(__a);
147 __a = _GLIBCXX_MOVE(__b);
148 __b = _GLIBCXX_MOVE(__tmp);
149 }
150
151 // _GLIBCXX_RESOLVE_LIB_DEFECTS
152 // DR 809. std::swap should be overloaded for array types.
153 template<typename _Tp, size_t _Nm>
154 inline void
155 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
156 // noexcept waits for c++/49045
157 {
158 for (size_t __n = 0; __n < _Nm; ++__n)
159 swap(__a[__n], __b[__n]);
160 }
161
162 _GLIBCXX_END_NAMESPACE_VERSION
163 } // namespace
164
165 #endif /* _MOVE_H */