forked from boostorg/move
Resolved conflicts when merging move_if_noexcept implementation
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <boost/type_traits/has_trivial_destructor.hpp>
|
||||
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
|
||||
#include <boost/type_traits/is_nothrow_move_assignable.hpp>
|
||||
#include <boost/type_traits/is_copy_constructible.hpp>
|
||||
#include <boost/move/detail/meta_utils.hpp>
|
||||
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
@@ -55,6 +56,15 @@ struct has_nothrow_move
|
||||
|
||||
namespace move_detail {
|
||||
|
||||
template <class T>
|
||||
struct has_nothrow_move_or_uncopyable
|
||||
: public ::boost::move_detail::integral_constant
|
||||
< bool
|
||||
, has_nothrow_move<T>::value ||
|
||||
!boost::is_copy_constructible<T>::value
|
||||
>
|
||||
{};
|
||||
|
||||
// Code from Jeffrey Lee Hellrung, many thanks
|
||||
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
|
@@ -18,6 +18,8 @@
|
||||
#include <boost/move/core.hpp>
|
||||
#include <boost/move/detail/meta_utils.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/move/traits.hpp>
|
||||
#include <boost/type_traits/add_const.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_MOVE_DOXYGEN_INVOKED)
|
||||
|
||||
@@ -81,6 +83,56 @@
|
||||
return x;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// move_if_noexcept()
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< enable_move_utility_emulation<T>::value && !has_move_emulation_enabled<T>::value, typename add_const<T>::type & >::type
|
||||
move_if_noexcept(T& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< enable_move_utility_emulation<T>::value && has_move_emulation_enabled<T>::value
|
||||
&& ::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value, rv<T>&>::type
|
||||
move_if_noexcept(T& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return *static_cast<rv<T>* >(::boost::move_detail::addressof(x));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< enable_move_utility_emulation<T>::value && has_move_emulation_enabled<T>::value
|
||||
&& ::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value, rv<T>&>::type
|
||||
move_if_noexcept(rv<T>& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< enable_move_utility_emulation<T>::value && has_move_emulation_enabled<T>::value
|
||||
&& !::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value, typename add_const<T>::type & >::type
|
||||
move_if_noexcept(T& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< enable_move_utility_emulation<T>::value && has_move_emulation_enabled<T>::value
|
||||
&& !::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value, typename add_const<T>::type & >::type
|
||||
move_if_noexcept(rv<T>& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
} //namespace boost
|
||||
|
||||
#else //#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_MOVE_DOXYGEN_INVOKED)
|
||||
@@ -92,6 +144,7 @@
|
||||
|
||||
using ::std::move;
|
||||
using ::std::forward;
|
||||
using ::std::move_if_noexcept;
|
||||
|
||||
} //namespace boost
|
||||
|
||||
@@ -183,6 +236,60 @@
|
||||
|
||||
#endif //BOOST_MOVE_DOXYGEN_INVOKED
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// move_if_noexcept()
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
#if defined(BOOST_MOVE_DOXYGEN_INVOKED)
|
||||
//! This function provides a way to convert a reference into a rvalue reference
|
||||
//! in compilers with rvalue references. For other compilers converts T & into
|
||||
//! <i>::boost::rv<T> &</i> so that move emulation is activated. Reference
|
||||
//! would be converted to rvalue reference only if input type is nothrow move
|
||||
//! constructible or if it has no copy constructor. In all other cases const
|
||||
//! reference would be returned
|
||||
template <class T>
|
||||
rvalue_reference move_if_noexcept(input_reference) noexcept;
|
||||
|
||||
#elif defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
|
||||
|
||||
//Old move approach, lvalues could bind to rvalue references
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< ::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value, typename remove_reference<T>::type&&>::type
|
||||
move_if_noexcept(T& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return ::boost::move(x);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< !::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value,
|
||||
typename add_const<typename remove_reference<T>::type>::type&
|
||||
>::type
|
||||
move_if_noexcept(T& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
#else //BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< ::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value, T&&>::type
|
||||
move_if_noexcept(T& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return ::boost::move(x);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline typename ::boost::move_detail::enable_if_c
|
||||
< !::boost::move_detail::has_nothrow_move_or_uncopyable<T>::value, typename add_const<T>::type & >::type
|
||||
move_if_noexcept(T& x) BOOST_NOEXCEPT
|
||||
{
|
||||
return x;
|
||||
}
|
||||
#endif //BOOST_MOVE_DOXYGEN_INVOKED
|
||||
|
||||
} //namespace boost {
|
||||
|
||||
#endif //#if defined(BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
|
||||
|
228
test/move_if_noexcept.cpp
Normal file
228
test/move_if_noexcept.cpp
Normal file
@@ -0,0 +1,228 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Antony Polukhin 2014.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See http://www.boost.org/libs/move for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
#include <boost/move/detail/config_begin.hpp>
|
||||
#include <boost/move/utility.hpp>
|
||||
#include "../example/movable.hpp"
|
||||
#include "../example/copymovable.hpp"
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//A copy_movable_noexcept class
|
||||
class copy_movable_noexcept
|
||||
{
|
||||
BOOST_COPYABLE_AND_MOVABLE(copy_movable_noexcept)
|
||||
int value_;
|
||||
|
||||
public:
|
||||
copy_movable_noexcept() : value_(1){}
|
||||
|
||||
//Move constructor and assignment
|
||||
copy_movable_noexcept(BOOST_RV_REF(copy_movable_noexcept) m)
|
||||
{ value_ = m.value_; m.value_ = 0; }
|
||||
|
||||
copy_movable_noexcept(const copy_movable_noexcept &m)
|
||||
{ value_ = m.value_; }
|
||||
|
||||
copy_movable_noexcept & operator=(BOOST_RV_REF(copy_movable_noexcept) m)
|
||||
{ value_ = m.value_; m.value_ = 0; return *this; }
|
||||
|
||||
copy_movable_noexcept & operator=(BOOST_COPY_ASSIGN_REF(copy_movable_noexcept) m)
|
||||
{ value_ = m.value_; return *this; }
|
||||
|
||||
bool moved() const //Observer
|
||||
{ return value_ == 0; }
|
||||
};
|
||||
|
||||
namespace boost{
|
||||
|
||||
template<>
|
||||
struct has_nothrow_move<copy_movable_noexcept>
|
||||
{
|
||||
static const bool value = true;
|
||||
};
|
||||
|
||||
} //namespace boost{
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//A movable_throwable class
|
||||
class movable_throwable
|
||||
{
|
||||
BOOST_MOVABLE_BUT_NOT_COPYABLE(movable_throwable)
|
||||
int value_;
|
||||
|
||||
public:
|
||||
movable_throwable() : value_(1){}
|
||||
|
||||
//Move constructor and assignment
|
||||
movable_throwable(BOOST_RV_REF(movable_throwable) m)
|
||||
{ value_ = m.value_; m.value_ = 0; }
|
||||
|
||||
movable_throwable & operator=(BOOST_RV_REF(movable_throwable) m)
|
||||
{ value_ = m.value_; m.value_ = 0; return *this; }
|
||||
|
||||
bool moved() const //Observer
|
||||
{ return !value_; }
|
||||
|
||||
int value() const //Observer
|
||||
{ return value_; }
|
||||
};
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Helper functions
|
||||
movable function(movable m)
|
||||
{
|
||||
return movable(boost::move_if_noexcept(m));
|
||||
}
|
||||
|
||||
copy_movable function(copy_movable m)
|
||||
{
|
||||
return copy_movable(boost::move_if_noexcept(m));
|
||||
}
|
||||
|
||||
copy_movable_noexcept function(copy_movable_noexcept m)
|
||||
{
|
||||
return copy_movable_noexcept(boost::move_if_noexcept(m));
|
||||
}
|
||||
|
||||
movable_throwable function(movable_throwable m)
|
||||
{
|
||||
return movable_throwable(boost::move_if_noexcept(m));
|
||||
}
|
||||
|
||||
movable functionr(BOOST_RV_REF(movable) m)
|
||||
{
|
||||
return movable(boost::move_if_noexcept(m));
|
||||
}
|
||||
|
||||
movable function2(movable m)
|
||||
{
|
||||
return boost::move_if_noexcept(m);
|
||||
}
|
||||
|
||||
BOOST_RV_REF(movable) function2r(BOOST_RV_REF(movable) m)
|
||||
{
|
||||
return boost::move_if_noexcept(m);
|
||||
}
|
||||
|
||||
movable move_return_function2 ()
|
||||
{
|
||||
return movable();
|
||||
}
|
||||
|
||||
movable move_return_function ()
|
||||
{
|
||||
movable m;
|
||||
return (boost::move_if_noexcept(m));
|
||||
}
|
||||
|
||||
#define BOOST_CHECK(x) if (!(x)) { return __LINE__; }
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
movable m;
|
||||
movable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
movable m3(function(movable(boost::move_if_noexcept(m2))));
|
||||
BOOST_CHECK(m2.moved());
|
||||
movable m4(function(boost::move_if_noexcept(m3)));
|
||||
BOOST_CHECK(m3.moved());
|
||||
BOOST_CHECK(!m4.moved());
|
||||
}
|
||||
{
|
||||
movable m;
|
||||
movable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
movable m3(functionr(movable(boost::move_if_noexcept(m2))));
|
||||
BOOST_CHECK(m2.moved());
|
||||
movable m4(functionr(boost::move_if_noexcept(m3)));
|
||||
BOOST_CHECK(m3.moved());
|
||||
BOOST_CHECK(!m4.moved());
|
||||
}
|
||||
{
|
||||
movable m;
|
||||
movable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
movable m3(function2(movable(boost::move_if_noexcept(m2))));
|
||||
BOOST_CHECK(m2.moved());
|
||||
movable m4(function2(boost::move_if_noexcept(m3)));
|
||||
BOOST_CHECK(m3.moved());
|
||||
BOOST_CHECK(!m4.moved());
|
||||
}
|
||||
{
|
||||
movable m;
|
||||
movable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
movable m3(function2r(movable(boost::move_if_noexcept(m2))));
|
||||
BOOST_CHECK(m2.moved());
|
||||
movable m4(function2r(boost::move_if_noexcept(m3)));
|
||||
BOOST_CHECK(m3.moved());
|
||||
BOOST_CHECK(!m4.moved());
|
||||
}
|
||||
{
|
||||
movable m;
|
||||
movable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
BOOST_CHECK(!m2.moved());
|
||||
movable m3(move_return_function());
|
||||
BOOST_CHECK(!m3.moved());
|
||||
}
|
||||
{
|
||||
movable m;
|
||||
movable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
BOOST_CHECK(!m2.moved());
|
||||
movable m3(move_return_function2());
|
||||
BOOST_CHECK(!m3.moved());
|
||||
}
|
||||
|
||||
// copy_movable may throw during move, so it must be copied
|
||||
{
|
||||
copy_movable m;
|
||||
copy_movable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(!m.moved());
|
||||
copy_movable m3(function(copy_movable(boost::move_if_noexcept(m2))));
|
||||
BOOST_CHECK(!m2.moved());
|
||||
copy_movable m4(function(boost::move_if_noexcept(m3)));
|
||||
BOOST_CHECK(!m3.moved());
|
||||
BOOST_CHECK(!m4.moved());
|
||||
}
|
||||
|
||||
|
||||
// copy_movable_noexcept can not throw during move
|
||||
{
|
||||
copy_movable_noexcept m;
|
||||
copy_movable_noexcept m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
copy_movable_noexcept m3(function(copy_movable_noexcept(boost::move_if_noexcept(m2))));
|
||||
BOOST_CHECK(m2.moved());
|
||||
copy_movable_noexcept m4(function(boost::move_if_noexcept(m3)));
|
||||
BOOST_CHECK(m3.moved());
|
||||
BOOST_CHECK(!m4.moved());
|
||||
}
|
||||
|
||||
// movable_throwable can not throw during move but it has no copy constructor
|
||||
{
|
||||
movable_throwable m;
|
||||
movable_throwable m2(boost::move_if_noexcept(m));
|
||||
BOOST_CHECK(m.moved());
|
||||
movable_throwable m3(function(movable_throwable(boost::move_if_noexcept(m2))));
|
||||
BOOST_CHECK(m2.moved());
|
||||
movable_throwable m4(function(boost::move_if_noexcept(m3)));
|
||||
BOOST_CHECK(m3.moved());
|
||||
BOOST_CHECK(!m4.moved());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include <boost/move/detail/config_end.hpp>
|
Reference in New Issue
Block a user