1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2012-2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/move for documentation.
10 //////////////////////////////////////////////////////////////////////////////
13 //! This header defines core utilities to ease the development
14 //! of move-aware functions. This header minimizes dependencies
15 //! from other libraries.
17 #ifndef BOOST_MOVE_MOVE_UTILITY_CORE_HPP
18 #define BOOST_MOVE_MOVE_UTILITY_CORE_HPP
20 #ifndef BOOST_CONFIG_HPP
21 # include <boost/config.hpp>
24 #if defined(BOOST_HAS_PRAGMA_ONCE)
28 #include <boost/move/detail/config_begin.hpp>
29 #include <boost/move/detail/workaround.hpp> //forceinline
30 #include <boost/move/core.hpp>
31 #include <boost/move/detail/meta_utils.hpp>
32 #include <boost/static_assert.hpp>
34 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_MOVE_DOXYGEN_INVOKED)
39 struct enable_move_utility_emulation
41 static const bool value = true;
44 //////////////////////////////////////////////////////////////////////////////
48 //////////////////////////////////////////////////////////////////////////////
51 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
53 , enable_move_utility_emulation<T>
54 , has_move_emulation_disabled<T>
56 move(T& x) BOOST_NOEXCEPT
62 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
64 , enable_move_utility_emulation<T>
65 , has_move_emulation_enabled<T>
67 move(T& x) BOOST_NOEXCEPT
69 return *BOOST_MOVE_TO_RV_CAST(::boost::rv<T>*, ::boost::move_detail::addressof(x) );
73 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
75 , enable_move_utility_emulation<T>
76 , has_move_emulation_enabled<T>
78 move(rv<T>& x) BOOST_NOEXCEPT
83 //////////////////////////////////////////////////////////////////////////////
87 //////////////////////////////////////////////////////////////////////////////
90 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
92 , enable_move_utility_emulation<T>
93 , ::boost::move_detail::is_rv<T>
95 forward(const typename ::boost::move_detail::identity<T>::type &x) BOOST_NOEXCEPT
97 return const_cast<T&>(x);
101 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
103 , enable_move_utility_emulation<T>
104 , ::boost::move_detail::is_not_rv<T>
106 forward(const typename ::boost::move_detail::identity<T>::type &x) BOOST_NOEXCEPT
111 //////////////////////////////////////////////////////////////////////////////
113 // move_if_not_lvalue_reference()
115 //////////////////////////////////////////////////////////////////////////////
118 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
120 , enable_move_utility_emulation<T>
121 , ::boost::move_detail::is_rv<T>
123 move_if_not_lvalue_reference(const typename ::boost::move_detail::identity<T>::type &x) BOOST_NOEXCEPT
125 return const_cast<T&>(x);
129 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
130 < typename ::boost::move_detail::add_lvalue_reference<T>::type
131 , enable_move_utility_emulation<T>
132 , ::boost::move_detail::is_not_rv<T>
133 , ::boost::move_detail::or_
134 < ::boost::move_detail::is_lvalue_reference<T>
135 , has_move_emulation_disabled<T>
138 move_if_not_lvalue_reference(typename ::boost::move_detail::remove_reference<T>::type &x) BOOST_NOEXCEPT
144 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_and
146 , enable_move_utility_emulation<T>
147 , ::boost::move_detail::is_not_rv<T>
148 , ::boost::move_detail::and_
149 < ::boost::move_detail::not_< ::boost::move_detail::is_lvalue_reference<T> >
150 , has_move_emulation_enabled<T>
153 move_if_not_lvalue_reference(typename ::boost::move_detail::remove_reference<T>::type &x) BOOST_NOEXCEPT
160 #else //#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_MOVE_DOXYGEN_INVOKED)
162 #if defined(BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
168 using ::std::forward;
172 #else //!BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE
176 //! This trait's internal boolean `value` is false in compilers with rvalue references
177 //! and true in compilers without rvalue references.
179 //! A user can specialize this trait for a type T to false to SFINAE out `move` and `forward`
180 //! so that the user can define a different move emulation for that type in namespace boost
181 //! (e.g. another Boost library for its types) and avoid any overload ambiguity.
183 struct enable_move_utility_emulation
185 static const bool value = false;
188 //////////////////////////////////////////////////////////////////////////////
192 //////////////////////////////////////////////////////////////////////////////
194 #if defined(BOOST_MOVE_DOXYGEN_INVOKED)
195 //! This function provides a way to convert a reference into a rvalue reference
196 //! in compilers with rvalue references. For other compilers if `T` is Boost.Move
197 //! enabled type then it converts `T&` into <tt>::boost::rv<T> &</tt> so that
198 //! move emulation is activated, else it returns `T &`.
200 rvalue_reference move(input_reference) noexcept;
202 #elif defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
204 //Old move approach, lvalues could bind to rvalue references
206 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::remove_reference<T>::type && move(T&& t) BOOST_NOEXCEPT
209 #else //BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES
212 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::remove_reference<T>::type && move(T&& t) BOOST_NOEXCEPT
213 { return static_cast<typename ::boost::move_detail::remove_reference<T>::type &&>(t); }
215 #endif //BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES
217 //////////////////////////////////////////////////////////////////////////////
221 //////////////////////////////////////////////////////////////////////////////
224 #if defined(BOOST_MOVE_DOXYGEN_INVOKED)
225 //! This function provides limited form of forwarding that is usually enough for
226 //! in-place construction and avoids the exponential overloading for
227 //! achieve the limited forwarding in C++03.
229 //! For compilers with rvalue references this function provides perfect forwarding.
232 //! * If input_reference binds to const ::boost::rv<T> & then it output_reference is
235 //! * Else, output_reference is equal to input_reference.
236 template <class T> output_reference forward(input_reference) noexcept;
237 #elif defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
239 //Old move approach, lvalues could bind to rvalue references
242 BOOST_MOVE_FORCEINLINE T&& forward(typename ::boost::move_detail::identity<T>::type&& t) BOOST_NOEXCEPT
248 BOOST_MOVE_FORCEINLINE T&& forward(typename ::boost::move_detail::remove_reference<T>::type& t) BOOST_NOEXCEPT
249 { return static_cast<T&&>(t); }
252 BOOST_MOVE_FORCEINLINE T&& forward(typename ::boost::move_detail::remove_reference<T>::type&& t) BOOST_NOEXCEPT
254 //"boost::forward<T> error: 'T' is a lvalue reference, can't forward as rvalue.";
255 BOOST_STATIC_ASSERT(!boost::move_detail::is_lvalue_reference<T>::value);
256 return static_cast<T&&>(t);
259 #endif //BOOST_MOVE_DOXYGEN_INVOKED
261 } //namespace boost {
263 #endif //BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE
265 //////////////////////////////////////////////////////////////////////////////
267 // move_if_not_lvalue_reference
269 //////////////////////////////////////////////////////////////////////////////
273 #if defined(BOOST_MOVE_DOXYGEN_INVOKED)
274 //! <b>Effects</b>: Calls `boost::move` if `input_reference` is not a lvalue reference.
275 //! Otherwise returns the reference
276 template <class T> output_reference move_if_not_lvalue_reference(input_reference) noexcept;
277 #elif defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
279 //Old move approach, lvalues could bind to rvalue references
282 BOOST_MOVE_FORCEINLINE T&& move_if_not_lvalue_reference(typename ::boost::move_detail::identity<T>::type&& t) BOOST_NOEXCEPT
288 BOOST_MOVE_FORCEINLINE T&& move_if_not_lvalue_reference(typename ::boost::move_detail::remove_reference<T>::type& t) BOOST_NOEXCEPT
289 { return static_cast<T&&>(t); }
292 BOOST_MOVE_FORCEINLINE T&& move_if_not_lvalue_reference(typename ::boost::move_detail::remove_reference<T>::type&& t) BOOST_NOEXCEPT
294 //"boost::forward<T> error: 'T' is a lvalue reference, can't forward as rvalue.";
295 BOOST_STATIC_ASSERT(!boost::move_detail::is_lvalue_reference<T>::value);
296 return static_cast<T&&>(t);
299 #endif //BOOST_MOVE_DOXYGEN_INVOKED
301 } //namespace boost {
303 #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
305 #if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
308 namespace move_detail{
310 template <typename T>
311 typename boost::move_detail::add_rvalue_reference<T>::type declval();
313 } //namespace move_detail{
316 #endif //#if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
319 #include <boost/move/detail/config_end.hpp>
321 #endif //#ifndef BOOST_MOVE_MOVE_UTILITY_CORE_HPP