1 // Move, forward and identity for C++11 + swap -*- C++ -*-
3 // Copyright (C) 2007-2020 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{utility}
33 #include <bits/c++config.h>
34 #if __cplusplus < 201103L
35 # include <bits/concept_check.h>
38 namespace std
_GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 // Used, in C++03 mode too, by allocators, etc.
44 * @brief Same as C++11 std::addressof
47 template<typename _Tp
>
48 inline _GLIBCXX_CONSTEXPR _Tp
*
49 __addressof(_Tp
& __r
) _GLIBCXX_NOEXCEPT
50 { return __builtin_addressof(__r
); }
52 #if __cplusplus >= 201103L
54 _GLIBCXX_END_NAMESPACE_VERSION
57 #include <type_traits> // Brings in std::declval too.
59 namespace std
_GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 * @addtogroup utilities
69 * @brief Forward an lvalue.
70 * @return The parameter cast to the specified type.
72 * This function is used to implement "perfect forwarding".
74 template<typename _Tp
>
76 forward(typename
std::remove_reference
<_Tp
>::type
& __t
) noexcept
77 { return static_cast<_Tp
&&>(__t
); }
80 * @brief Forward an rvalue.
81 * @return The parameter cast to the specified type.
83 * This function is used to implement "perfect forwarding".
85 template<typename _Tp
>
87 forward(typename
std::remove_reference
<_Tp
>::type
&& __t
) noexcept
89 static_assert(!std::is_lvalue_reference
<_Tp
>::value
, "template argument"
90 " substituting _Tp is an lvalue reference type");
91 return static_cast<_Tp
&&>(__t
);
95 * @brief Convert a value to an rvalue.
96 * @param __t A thing of arbitrary type.
97 * @return The parameter cast to an rvalue-reference to allow moving it.
99 template<typename _Tp
>
100 constexpr typename
std::remove_reference
<_Tp
>::type
&&
101 move(_Tp
&& __t
) noexcept
102 { return static_cast<typename
std::remove_reference
<_Tp
>::type
&&>(__t
); }
105 template<typename _Tp
>
106 struct __move_if_noexcept_cond
107 : public __and_
<__not_
<is_nothrow_move_constructible
<_Tp
>>,
108 is_copy_constructible
<_Tp
>>::type
{ };
111 * @brief Conditionally convert a value to an rvalue.
112 * @param __x A thing of arbitrary type.
113 * @return The parameter, possibly cast to an rvalue-reference.
115 * Same as std::move unless the type's move constructor could throw and the
116 * type is copyable, in which case an lvalue-reference is returned instead.
118 template<typename _Tp
>
120 conditional
<__move_if_noexcept_cond
<_Tp
>::value
, const _Tp
&, _Tp
&&>::type
121 move_if_noexcept(_Tp
& __x
) noexcept
122 { return std::move(__x
); }
124 // declval, from type_traits.
126 #if __cplusplus > 201402L
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // 2296. std::addressof should be constexpr
129 # define __cpp_lib_addressof_constexpr 201603
132 * @brief Returns the actual address of the object or function
133 * referenced by r, even in the presence of an overloaded
135 * @param __r Reference to an object or function.
136 * @return The actual address.
138 template<typename _Tp
>
139 inline _GLIBCXX17_CONSTEXPR _Tp
*
140 addressof(_Tp
& __r
) noexcept
141 { return std::__addressof(__r
); }
143 // _GLIBCXX_RESOLVE_LIB_DEFECTS
144 // 2598. addressof works on temporaries
145 template<typename _Tp
>
146 const _Tp
* addressof(const _Tp
&&) = delete;
148 // C++11 version of std::exchange for internal use.
149 template <typename _Tp
, typename _Up
= _Tp
>
152 __exchange(_Tp
& __obj
, _Up
&& __new_val
)
154 _Tp __old_val
= std::move(__obj
);
155 __obj
= std::forward
<_Up
>(__new_val
);
159 /// @} group utilities
161 #define _GLIBCXX_FWDREF(_Tp) _Tp&&
162 #define _GLIBCXX_MOVE(__val) std::move(__val)
163 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
165 #define _GLIBCXX_FWDREF(_Tp) const _Tp&
166 #define _GLIBCXX_MOVE(__val) (__val)
167 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
171 * @addtogroup utilities
176 * @brief Swaps two values.
177 * @param __a A thing of arbitrary type.
178 * @param __b Another thing of arbitrary type.
181 template<typename _Tp
>
184 #if __cplusplus >= 201103L
185 typename enable_if
<__and_
<__not_
<__is_tuple_like
<_Tp
>>,
186 is_move_constructible
<_Tp
>,
187 is_move_assignable
<_Tp
>>::value
>::type
191 swap(_Tp
& __a
, _Tp
& __b
)
192 _GLIBCXX_NOEXCEPT_IF(__and_
<is_nothrow_move_constructible
<_Tp
>,
193 is_nothrow_move_assignable
<_Tp
>>::value
)
195 #if __cplusplus < 201103L
196 // concept requirements
197 __glibcxx_function_requires(_SGIAssignableConcept
<_Tp
>)
199 _Tp __tmp
= _GLIBCXX_MOVE(__a
);
200 __a
= _GLIBCXX_MOVE(__b
);
201 __b
= _GLIBCXX_MOVE(__tmp
);
204 // _GLIBCXX_RESOLVE_LIB_DEFECTS
205 // DR 809. std::swap should be overloaded for array types.
206 /// Swap the contents of two arrays.
207 template<typename _Tp
, size_t _Nm
>
210 #if __cplusplus >= 201103L
211 typename enable_if
<__is_swappable
<_Tp
>::value
>::type
215 swap(_Tp (&__a
)[_Nm
], _Tp (&__b
)[_Nm
])
216 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable
<_Tp
>::value
)
218 for (size_t __n
= 0; __n
< _Nm
; ++__n
)
219 swap(__a
[__n
], __b
[__n
]);
222 /// @} group utilities
223 _GLIBCXX_END_NAMESPACE_VERSION