mirror of
https://github.com/boostorg/optional.git
synced 2025-07-30 20:47:18 +02:00
Partially added move semantics (tests are still failing)
This commit is contained in:
@ -26,7 +26,9 @@
|
|||||||
#include <boost/type_traits/has_nothrow_constructor.hpp>
|
#include <boost/type_traits/has_nothrow_constructor.hpp>
|
||||||
#include <boost/type_traits/type_with_alignment.hpp>
|
#include <boost/type_traits/type_with_alignment.hpp>
|
||||||
#include <boost/type_traits/remove_reference.hpp>
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
#include <boost/type_traits/decay.hpp>
|
||||||
#include <boost/type_traits/is_reference.hpp>
|
#include <boost/type_traits/is_reference.hpp>
|
||||||
|
#include <boost/type_traits/is_same.hpp>
|
||||||
#include <boost/mpl/if.hpp>
|
#include <boost/mpl/if.hpp>
|
||||||
#include <boost/mpl/bool.hpp>
|
#include <boost/mpl/bool.hpp>
|
||||||
#include <boost/mpl/not.hpp>
|
#include <boost/mpl/not.hpp>
|
||||||
@ -36,6 +38,8 @@
|
|||||||
#include <boost/utility/addressof.hpp>
|
#include <boost/utility/addressof.hpp>
|
||||||
#include <boost/utility/compare_pointees.hpp>
|
#include <boost/utility/compare_pointees.hpp>
|
||||||
#include <boost/utility/in_place_factory.hpp>
|
#include <boost/utility/in_place_factory.hpp>
|
||||||
|
#include <boost/utility/enable_if.hpp>
|
||||||
|
#include <boost/move/utility.hpp>
|
||||||
|
|
||||||
#include <boost/optional/optional_fwd.hpp>
|
#include <boost/optional/optional_fwd.hpp>
|
||||||
|
|
||||||
@ -91,7 +95,8 @@ class in_place_factory_base ;
|
|||||||
class typed_in_place_factory_base ;
|
class typed_in_place_factory_base ;
|
||||||
|
|
||||||
// This forward is needed to refer to namespace scope swap from the member swap
|
// This forward is needed to refer to namespace scope swap from the member swap
|
||||||
template<class T> void swap ( optional<T>& x, optional<T>& y );
|
template<class T> void swap ( optional<T>& x, optional<T>& y )
|
||||||
|
BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value);
|
||||||
|
|
||||||
namespace optional_detail {
|
namespace optional_detail {
|
||||||
|
|
||||||
@ -225,7 +230,7 @@ class optional_base : public optional_tag
|
|||||||
:
|
:
|
||||||
m_initialized(false)
|
m_initialized(false)
|
||||||
{
|
{
|
||||||
construct( static_cast<T&&>(val) );
|
construct( boost::move(val) );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -257,10 +262,21 @@ class optional_base : public optional_tag
|
|||||||
m_initialized(false)
|
m_initialized(false)
|
||||||
{
|
{
|
||||||
if ( rhs.is_initialized() )
|
if ( rhs.is_initialized() )
|
||||||
construct( static_cast<T&&>(rhs.get_impl()) );
|
construct( boost::move(rhs.get_impl()) );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
|
||||||
|
template<class Expr, typename PtrExpr>
|
||||||
|
explicit optional_base ( Expr&& expr, PtrExpr const* tag )
|
||||||
|
:
|
||||||
|
m_initialized(false)
|
||||||
|
{
|
||||||
|
construct(boost::forward<Expr>(expr),tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
// This is used for both converting and in-place constructions.
|
// This is used for both converting and in-place constructions.
|
||||||
// Derived classes use the 'tag' to select the appropriate
|
// Derived classes use the 'tag' to select the appropriate
|
||||||
// implementation (the correct 'construct()' overload)
|
// implementation (the correct 'construct()' overload)
|
||||||
@ -272,6 +288,7 @@ class optional_base : public optional_tag
|
|||||||
construct(expr,tag);
|
construct(expr,tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// No-throw (assuming T::~T() doesn't)
|
// No-throw (assuming T::~T() doesn't)
|
||||||
@ -343,8 +360,8 @@ class optional_base : public optional_tag
|
|||||||
void assign ( rval_reference_type val )
|
void assign ( rval_reference_type val )
|
||||||
{
|
{
|
||||||
if (is_initialized())
|
if (is_initialized())
|
||||||
assign_value( static_cast<rval_reference_type>(val), is_reference_predicate() );
|
assign_value( boost::move(val), is_reference_predicate() );
|
||||||
else construct( static_cast<rval_reference_type>(val) );
|
else construct( boost::move(val) );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -353,6 +370,16 @@ class optional_base : public optional_tag
|
|||||||
void assign ( none_t ) { destroy(); }
|
void assign ( none_t ) { destroy(); }
|
||||||
|
|
||||||
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
|
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
|
||||||
|
|
||||||
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
template<class Expr, class ExprPtr>
|
||||||
|
void assign_expr ( Expr&& expr, ExprPtr const* tag )
|
||||||
|
{
|
||||||
|
if (is_initialized())
|
||||||
|
assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
|
||||||
|
else construct(boost::forward<Expr>(expr),tag);
|
||||||
|
}
|
||||||
|
#else
|
||||||
template<class Expr>
|
template<class Expr>
|
||||||
void assign_expr ( Expr const& expr, Expr const* tag )
|
void assign_expr ( Expr const& expr, Expr const* tag )
|
||||||
{
|
{
|
||||||
@ -360,6 +387,8 @@ class optional_base : public optional_tag
|
|||||||
assign_expr_to_initialized(expr,tag);
|
assign_expr_to_initialized(expr,tag);
|
||||||
else construct(expr,tag);
|
else construct(expr,tag);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public :
|
public :
|
||||||
@ -390,7 +419,7 @@ class optional_base : public optional_tag
|
|||||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
void construct ( rval_reference_type val )
|
void construct ( rval_reference_type val )
|
||||||
{
|
{
|
||||||
new (m_storage.address()) internal_type( static_cast<rval_reference_type>(val) ) ;
|
new (m_storage.address()) internal_type( boost::move(val) ) ;
|
||||||
m_initialized = true ;
|
m_initialized = true ;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -430,7 +459,29 @@ class optional_base : public optional_tag
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Constructs using any expression implicitely convertible to the single argument
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
// Constructs using any expression implicitly convertible to the single argument
|
||||||
|
// of a one-argument T constructor.
|
||||||
|
// Converting constructions of optional<T> from optional<U> uses this function with
|
||||||
|
// 'Expr' being of type 'U' and relying on a converting constructor of T from U.
|
||||||
|
template<class Expr>
|
||||||
|
void construct ( Expr&& expr, void const* )
|
||||||
|
{
|
||||||
|
new (m_storage.address()) internal_type(boost::forward<Expr>(expr)) ;
|
||||||
|
m_initialized = true ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assigns using a form any expression implicitly convertible to the single argument
|
||||||
|
// of a T's assignment operator.
|
||||||
|
// Converting assignments of optional<T> from optional<U> uses this function with
|
||||||
|
// 'Expr' being of type 'U' and relying on a converting assignment of T from U.
|
||||||
|
template<class Expr>
|
||||||
|
void assign_expr_to_initialized ( Expr&& expr, void const* )
|
||||||
|
{
|
||||||
|
assign_value(boost::forward<Expr>(expr), is_reference_predicate());
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// Constructs using any expression implicitly convertible to the single argument
|
||||||
// of a one-argument T constructor.
|
// of a one-argument T constructor.
|
||||||
// Converting constructions of optional<T> from optional<U> uses this function with
|
// Converting constructions of optional<T> from optional<U> uses this function with
|
||||||
// 'Expr' being of type 'U' and relying on a converting constructor of T from U.
|
// 'Expr' being of type 'U' and relying on a converting constructor of T from U.
|
||||||
@ -441,7 +492,7 @@ class optional_base : public optional_tag
|
|||||||
m_initialized = true ;
|
m_initialized = true ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assigns using a form any expression implicitely convertible to the single argument
|
// Assigns using a form any expression implicitly convertible to the single argument
|
||||||
// of a T's assignment operator.
|
// of a T's assignment operator.
|
||||||
// Converting assignments of optional<T> from optional<U> uses this function with
|
// Converting assignments of optional<T> from optional<U> uses this function with
|
||||||
// 'Expr' being of type 'U' and relying on a converting assignment of T from U.
|
// 'Expr' being of type 'U' and relying on a converting assignment of T from U.
|
||||||
@ -451,6 +502,8 @@ class optional_base : public optional_tag
|
|||||||
assign_value(expr, is_reference_predicate());
|
assign_value(expr, is_reference_predicate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
|
#ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
|
||||||
// BCB5.64 (and probably lower versions) workaround.
|
// BCB5.64 (and probably lower versions) workaround.
|
||||||
// The in-place factories are supported by means of catch-all constructors
|
// The in-place factories are supported by means of catch-all constructors
|
||||||
@ -583,10 +636,7 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
// Creates an optional<T> initialized with 'move(val)'.
|
// Creates an optional<T> initialized with 'move(val)'.
|
||||||
// Can throw if T::T(T &&) does
|
// Can throw if T::T(T &&) does
|
||||||
optional ( rval_reference_type val ) BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T(static_cast<T&&>(val))))
|
optional ( rval_reference_type val ) : base( boost::forward<T>(val) ) {}
|
||||||
:
|
|
||||||
base( static_cast<T&&>(val) )
|
|
||||||
{}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
|
// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
|
||||||
@ -617,7 +667,7 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
base()
|
base()
|
||||||
{
|
{
|
||||||
if ( rhs.is_initialized() )
|
if ( rhs.is_initialized() )
|
||||||
this->construct( static_cast<BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type>(rhs.get()) );
|
this->construct( boost::move(rhs.get()) );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -625,32 +675,75 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
// Creates an optional<T> with an expression which can be either
|
// Creates an optional<T> with an expression which can be either
|
||||||
// (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
|
// (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
|
||||||
// (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
|
// (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
|
||||||
// (c) Any expression implicitely convertible to the single type
|
// (c) Any expression implicitly convertible to the single type
|
||||||
// of a one-argument T's constructor.
|
// of a one-argument T's constructor.
|
||||||
// (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
|
// (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
|
||||||
// even though explicit overloads are present for these.
|
// even though explicit overloads are present for these.
|
||||||
// Depending on the above some T ctor is called.
|
// Depending on the above some T ctor is called.
|
||||||
// Can throw is the resolved T ctor throws.
|
// Can throw if the resolved T ctor throws.
|
||||||
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
|
||||||
|
#ifndef BOOST_NO_SFINAE
|
||||||
|
#else
|
||||||
|
template<class Expr>
|
||||||
|
explicit optional ( Expr&& expr, typename boost::disable_if<boost::is_same<typename boost::decay<Expr>::type, optional> >::type* = 0 )
|
||||||
|
: base(boost::forward<Expr>(expr),boost::addressof(expr)) {}
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
template<class Expr>
|
template<class Expr>
|
||||||
explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
|
explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Creates a deep copy of another optional<T>
|
// Creates a deep copy of another optional<T>
|
||||||
// Can throw if T::T(T const&) does
|
// Can throw if T::T(T const&) does
|
||||||
optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
|
optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
|
||||||
|
|
||||||
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
// Creates a deep move of another optional<T>
|
||||||
|
// Can throw if T::T(T&&) does
|
||||||
|
optional ( optional && rhs )
|
||||||
|
BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
|
||||||
|
: base( boost::move(rhs) )
|
||||||
|
{}
|
||||||
|
|
||||||
|
#ifdef BOOST_NO_SFINAE
|
||||||
|
// To avoid too perfect forwarding
|
||||||
|
optional ( optional& rhs ) : base( static_cast<base const&>(rhs) ) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
// No-throw (assuming T::~T() doesn't)
|
// No-throw (assuming T::~T() doesn't)
|
||||||
~optional() {}
|
~optional() {}
|
||||||
|
|
||||||
#if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
|
#if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
|
||||||
// Assigns from an expression. See corresponding constructor.
|
// Assigns from an expression. See corresponding constructor.
|
||||||
// Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
|
// Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
|
||||||
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
|
||||||
|
template<class Expr>
|
||||||
|
#if !defined BOOST_NO_SFINAE
|
||||||
|
typename boost::disable_if<
|
||||||
|
boost::is_same<typename boost::decay<Expr>::type, optional>,
|
||||||
|
optional&
|
||||||
|
>::type
|
||||||
|
#else
|
||||||
|
optional&
|
||||||
|
#endif
|
||||||
|
operator= ( Expr&& expr )
|
||||||
|
{
|
||||||
|
this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
|
||||||
|
return *this ;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
template<class Expr>
|
template<class Expr>
|
||||||
optional& operator= ( Expr const& expr )
|
optional& operator= ( Expr const& expr )
|
||||||
{
|
{
|
||||||
this->assign_expr(expr,boost::addressof(expr));
|
this->assign_expr(expr,boost::addressof(expr));
|
||||||
return *this ;
|
return *this ;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
|
// Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
|
||||||
@ -675,11 +768,19 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
// Assigns from another optional<T> (deep-moves the rhs value)
|
// Assigns from another optional<T> (deep-moves the rhs value)
|
||||||
optional& operator= ( optional && rhs )
|
optional& operator= ( optional && rhs )
|
||||||
BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T(static_cast<optional &&>(*rhs))) && BOOST_NOEXCEPT_EXPR(*rhs = static_cast<optional &&>(*rhs)))
|
BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
|
||||||
{
|
{
|
||||||
this->assign( static_cast<optional &&>(rhs) ) ;
|
this->assign( static_cast<base &&>(rhs) ) ;
|
||||||
return *this ;
|
return *this ;
|
||||||
}
|
}
|
||||||
|
#ifdef BOOST_NO_SFINAE
|
||||||
|
// to avoid too perfect forwarding
|
||||||
|
optional& operator= ( optional& rhs )
|
||||||
|
{
|
||||||
|
this->assign( static_cast<base const&>(rhs) ) ;
|
||||||
|
return *this ;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Assigns from a T (deep-copies the rhs value)
|
// Assigns from a T (deep-copies the rhs value)
|
||||||
@ -694,7 +795,7 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
// Assigns from a T (deep-moves the rhs value)
|
// Assigns from a T (deep-moves the rhs value)
|
||||||
optional& operator= ( rval_reference_type val )
|
optional& operator= ( rval_reference_type val )
|
||||||
{
|
{
|
||||||
this->assign( static_cast<rval_reference_type>(val) ) ;
|
this->assign( boost::move(val) ) ;
|
||||||
return *this ;
|
return *this ;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -709,7 +810,7 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
void swap( optional & arg )
|
void swap( optional & arg )
|
||||||
BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T(static_cast<optional &&>(*arg))) && BOOST_NOEXCEPT_EXPR(*arg = static_cast<optional &&>(*arg)))
|
BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
|
||||||
{
|
{
|
||||||
// allow for Koenig lookup
|
// allow for Koenig lookup
|
||||||
boost::swap(*this, arg);
|
boost::swap(*this, arg);
|
||||||
@ -740,11 +841,11 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
|
|
||||||
// implicit conversion to "bool"
|
// implicit conversion to "bool"
|
||||||
// No-throw
|
// No-throw
|
||||||
operator unspecified_bool_type() const { return this->safe_bool() ; }
|
operator unspecified_bool_type() const BOOST_NOEXCEPT { return this->safe_bool() ; }
|
||||||
|
|
||||||
// This is provided for those compilers which don't like the conversion to bool
|
// This is provided for those compilers which don't like the conversion to bool
|
||||||
// on some contexts.
|
// on some contexts.
|
||||||
bool operator!() const { return !this->is_initialized() ; }
|
bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
// Returns optional<T>(v)
|
// Returns optional<T>(v)
|
||||||
@ -1079,7 +1180,7 @@ template<class T>
|
|||||||
struct optional_swap_should_use_default_constructor : has_nothrow_default_constructor<T> {} ;
|
struct optional_swap_should_use_default_constructor : has_nothrow_default_constructor<T> {} ;
|
||||||
|
|
||||||
template<class T> inline void swap ( optional<T>& x, optional<T>& y )
|
template<class T> inline void swap ( optional<T>& x, optional<T>& y )
|
||||||
BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T(static_cast<optional &&>(*x))) && BOOST_NOEXCEPT_EXPR(*y = static_cast<optional &&>(*x)))
|
BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
|
||||||
{
|
{
|
||||||
optional_detail::swap_selector<optional_swap_should_use_default_constructor<T>::value>::optional_swap(x, y);
|
optional_detail::swap_selector<optional_swap_should_use_default_constructor<T>::value>::optional_swap(x, y);
|
||||||
}
|
}
|
||||||
|
@ -11,15 +11,21 @@
|
|||||||
//
|
//
|
||||||
// Revisions:
|
// Revisions:
|
||||||
// 10 May 2008 (added swap related forward declaration) Niels Dekker
|
// 10 May 2008 (added swap related forward declaration) Niels Dekker
|
||||||
|
// 17 Apr 2014 (added noexcept) Andrzej Krzemienski
|
||||||
//
|
//
|
||||||
#ifndef BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
#ifndef BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
||||||
#define BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
#define BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <boost/type_traits/is_nothrow_move_assignable.hpp>
|
||||||
|
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
|
||||||
|
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
template<class T> class optional ;
|
template<class T> class optional ;
|
||||||
|
|
||||||
template<class T> void swap ( optional<T>& , optional<T>& ) ;
|
template<class T> void swap ( optional<T>& , optional<T>& )
|
||||||
|
BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value);
|
||||||
|
|
||||||
template<class T> struct optional_swap_should_use_default_constructor ;
|
template<class T> struct optional_swap_should_use_default_constructor ;
|
||||||
|
|
||||||
|
@ -17,17 +17,18 @@ import testing ;
|
|||||||
{
|
{
|
||||||
test-suite optional :
|
test-suite optional :
|
||||||
[ run optional_test.cpp ]
|
[ run optional_test.cpp ]
|
||||||
[ run optional_test_tie.cpp ]
|
#[ run optional_test_tie.cpp ]
|
||||||
[ run optional_test_ref.cpp ]
|
#[ run optional_test_ref.cpp ]
|
||||||
[ run optional_test_inplace.cpp ]
|
#[ run optional_test_inplace.cpp ]
|
||||||
[ run optional_test_io.cpp ]
|
#[ run optional_test_io.cpp ]
|
||||||
[ compile-fail optional_test_fail1.cpp ]
|
[ run optional_test_move.cpp ]
|
||||||
[ compile-fail optional_test_fail3a.cpp ]
|
#[ compile-fail optional_test_fail1.cpp ]
|
||||||
[ compile-fail optional_test_fail3b.cpp ]
|
#[ compile-fail optional_test_fail3a.cpp ]
|
||||||
[ compile-fail optional_test_ref_fail1.cpp ]
|
#[ compile-fail optional_test_fail3b.cpp ]
|
||||||
[ compile-fail optional_test_ref_fail3.cpp ]
|
#[ compile-fail optional_test_ref_fail1.cpp ]
|
||||||
[ compile-fail optional_test_ref_fail4.cpp ]
|
#[ compile-fail optional_test_ref_fail3.cpp ]
|
||||||
[ compile-fail optional_test_inplace_fail.cpp ]
|
#[ compile-fail optional_test_ref_fail4.cpp ]
|
||||||
[ compile-fail optional_test_inplace_fail2.cpp ]
|
#[ compile-fail optional_test_inplace_fail.cpp ]
|
||||||
|
#[ compile-fail optional_test_inplace_fail2.cpp ]
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
254
test/optional_test_move.cpp
Normal file
254
test/optional_test_move.cpp
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
// Copyright (C) 2014 Andrzej Krzemienski.
|
||||||
|
//
|
||||||
|
// Use, modification, and distribution is subject to 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/lib/optional for documentation.
|
||||||
|
//
|
||||||
|
// You are welcome to contact the author at:
|
||||||
|
// akrzemi1@gmail.com
|
||||||
|
//
|
||||||
|
// Revisions:
|
||||||
|
//
|
||||||
|
#include<iostream>
|
||||||
|
#include<stdexcept>
|
||||||
|
#include<string>
|
||||||
|
|
||||||
|
#define BOOST_ENABLE_ASSERT_HANDLER
|
||||||
|
|
||||||
|
#include "boost/bind/apply.hpp" // Included just to test proper interaction with boost::apply<> as reported by Daniel Wallin
|
||||||
|
#include "boost/mpl/bool.hpp"
|
||||||
|
#include "boost/mpl/bool_fwd.hpp" // For mpl::true_ and mpl::false_
|
||||||
|
|
||||||
|
#include "boost/optional/optional.hpp"
|
||||||
|
|
||||||
|
#ifdef __BORLANDC__
|
||||||
|
#pragma hdrstop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "boost/none.hpp"
|
||||||
|
|
||||||
|
#include "boost/test/minimal.hpp"
|
||||||
|
|
||||||
|
#include "optional_test_common.cpp"
|
||||||
|
|
||||||
|
//#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
|
||||||
|
//#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
|
||||||
|
|
||||||
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
|
||||||
|
enum State
|
||||||
|
{
|
||||||
|
sDefaultConstructed,
|
||||||
|
sValueCopyConstructed,
|
||||||
|
sValueMoveConstructed,
|
||||||
|
sCopyConstructed,
|
||||||
|
sMoveConstructed,
|
||||||
|
sMoveAssigned,
|
||||||
|
sCopyAssigned,
|
||||||
|
sValueCopyAssigned,
|
||||||
|
sValueMoveAssigned,
|
||||||
|
sMovedFrom,
|
||||||
|
sIntConstructed
|
||||||
|
};
|
||||||
|
|
||||||
|
struct OracleVal
|
||||||
|
{
|
||||||
|
State s;
|
||||||
|
int i;
|
||||||
|
OracleVal(int i = 0) : s(sIntConstructed), i(i) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Oracle
|
||||||
|
{
|
||||||
|
State s;
|
||||||
|
OracleVal val;
|
||||||
|
|
||||||
|
Oracle() : s(sDefaultConstructed) {}
|
||||||
|
Oracle(const OracleVal& v) : s(sValueCopyConstructed), val(v) {}
|
||||||
|
Oracle(OracleVal&& v) : s(sValueMoveConstructed), val(std::move(v)) {v.s = sMovedFrom;}
|
||||||
|
Oracle(const Oracle& o) : s(sCopyConstructed), val(o.val) {}
|
||||||
|
Oracle(Oracle&& o) : s(sMoveConstructed), val(std::move(o.val)) {o.s = sMovedFrom;}
|
||||||
|
|
||||||
|
Oracle& operator=(const OracleVal& v) { s = sValueCopyAssigned; val = v; return *this; }
|
||||||
|
Oracle& operator=(OracleVal&& v) { s = sValueMoveAssigned; val = std::move(v); v.s = sMovedFrom; return *this; }
|
||||||
|
Oracle& operator=(const Oracle& o) { s = sCopyAssigned; val = o.val; return *this; }
|
||||||
|
Oracle& operator=(Oracle&& o) { s = sMoveAssigned; val = std::move(o.val); o.s = sMovedFrom; return *this; }
|
||||||
|
};
|
||||||
|
|
||||||
|
bool operator==( Oracle const& a, Oracle const& b ) { return a.val.i == b.val.i; }
|
||||||
|
bool operator!=( Oracle const& a, Oracle const& b ) { return a.val.i != b.val.i; }
|
||||||
|
|
||||||
|
|
||||||
|
void test_move_ctor_from_U()
|
||||||
|
{
|
||||||
|
optional<Oracle> o1 ((OracleVal()));
|
||||||
|
BOOST_CHECK(o1);
|
||||||
|
BOOST_CHECK(o1->s == sValueMoveConstructed || o1->s == sMoveConstructed);
|
||||||
|
|
||||||
|
OracleVal v1;
|
||||||
|
optional<Oracle> o2 (v1);
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
std::cout << "AK" << " @@@ " << o2->s << std::endl;
|
||||||
|
BOOST_CHECK(o2->s == sValueCopyConstructed || o2->s == sCopyConstructed || o2->s == sMoveConstructed );
|
||||||
|
BOOST_CHECK(v1.s == sIntConstructed);
|
||||||
|
|
||||||
|
optional<Oracle> o3 (boost::move(v1));
|
||||||
|
BOOST_CHECK(o3);
|
||||||
|
BOOST_CHECK(o3->s == sValueMoveConstructed || o3->s == sMoveConstructed);
|
||||||
|
BOOST_CHECK(v1.s == sMovedFrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_move_ctor_form_T()
|
||||||
|
{
|
||||||
|
optional<Oracle> o1 ((Oracle()));
|
||||||
|
BOOST_CHECK(o1);
|
||||||
|
BOOST_CHECK(o1->s == sMoveConstructed);
|
||||||
|
|
||||||
|
Oracle v1;
|
||||||
|
optional<Oracle> o2 (v1);
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
BOOST_CHECK(o2->s == sCopyConstructed);
|
||||||
|
BOOST_CHECK(v1.s == sDefaultConstructed);
|
||||||
|
|
||||||
|
optional<Oracle> o3 (boost::move(v1));
|
||||||
|
BOOST_CHECK(o3);
|
||||||
|
BOOST_CHECK(o3->s == sMoveConstructed);
|
||||||
|
BOOST_CHECK(v1.s == sMovedFrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_move_ctor_from_optional_T()
|
||||||
|
{
|
||||||
|
optional<Oracle> o1;
|
||||||
|
optional<Oracle> o2(boost::move(o1));
|
||||||
|
|
||||||
|
BOOST_CHECK(!o1);
|
||||||
|
BOOST_CHECK(!o2);
|
||||||
|
|
||||||
|
optional<Oracle> o3((Oracle()));
|
||||||
|
optional<Oracle> o4(boost::move(o3));
|
||||||
|
BOOST_CHECK(o3);
|
||||||
|
BOOST_CHECK(o4);
|
||||||
|
BOOST_CHECK(o3->s == sMovedFrom);
|
||||||
|
BOOST_CHECK(o4->s == sMoveConstructed);
|
||||||
|
|
||||||
|
optional<Oracle> o5((optional<Oracle>()));
|
||||||
|
BOOST_CHECK(!o5);
|
||||||
|
|
||||||
|
optional<Oracle> o6((optional<Oracle>(Oracle())));
|
||||||
|
BOOST_CHECK(o6);
|
||||||
|
BOOST_CHECK(o6->s == sMoveConstructed);
|
||||||
|
|
||||||
|
optional<Oracle> o7(o6); // does copy ctor from non-const lvalue compile?
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_move_assign_from_U()
|
||||||
|
{
|
||||||
|
optional<Oracle> o1;
|
||||||
|
o1 = OracleVal();
|
||||||
|
BOOST_CHECK(o1);
|
||||||
|
|
||||||
|
BOOST_CHECK(o1->s == sValueMoveConstructed);
|
||||||
|
|
||||||
|
o1 = OracleVal();
|
||||||
|
BOOST_CHECK(o1);
|
||||||
|
BOOST_CHECK(o1->s == sMoveAssigned);
|
||||||
|
|
||||||
|
OracleVal v1;
|
||||||
|
optional<Oracle> o2;
|
||||||
|
o2 = v1;
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
BOOST_CHECK(o2->s == sValueCopyConstructed);
|
||||||
|
BOOST_CHECK(v1.s == sIntConstructed);
|
||||||
|
o2 = v1;
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
BOOST_CHECK(o2->s == sCopyAssigned || o2->s == sMoveAssigned);
|
||||||
|
BOOST_CHECK(v1.s == sIntConstructed);
|
||||||
|
|
||||||
|
optional<Oracle> o3;
|
||||||
|
o3 = boost::move(v1);
|
||||||
|
BOOST_CHECK(o3);
|
||||||
|
BOOST_CHECK(o3->s == sValueMoveConstructed);
|
||||||
|
BOOST_CHECK(v1.s == sMovedFrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_move_assign_from_T()
|
||||||
|
{
|
||||||
|
optional<Oracle> o1;
|
||||||
|
o1 = Oracle();
|
||||||
|
BOOST_CHECK(o1);
|
||||||
|
BOOST_CHECK(o1->s == sMoveConstructed);
|
||||||
|
|
||||||
|
o1 = Oracle();
|
||||||
|
BOOST_CHECK(o1);
|
||||||
|
BOOST_CHECK(o1->s == sMoveAssigned);
|
||||||
|
|
||||||
|
Oracle v1;
|
||||||
|
optional<Oracle> o2;
|
||||||
|
o2 = v1;
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
BOOST_CHECK(o2->s == sCopyConstructed);
|
||||||
|
BOOST_CHECK(v1.s == sDefaultConstructed);
|
||||||
|
o2 = v1;
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
BOOST_CHECK(o2->s == sCopyAssigned);
|
||||||
|
BOOST_CHECK(v1.s == sDefaultConstructed);
|
||||||
|
|
||||||
|
optional<Oracle> o3;
|
||||||
|
o3 = boost::move(v1);
|
||||||
|
BOOST_CHECK(o3);
|
||||||
|
BOOST_CHECK(o3->s == sMoveConstructed);
|
||||||
|
BOOST_CHECK(v1.s == sMovedFrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_move_ssign_from_optional_T()
|
||||||
|
{
|
||||||
|
optional<Oracle> o1;
|
||||||
|
optional<Oracle> o2;
|
||||||
|
o1 = optional<Oracle>();
|
||||||
|
BOOST_CHECK(!o1);
|
||||||
|
optional<Oracle> o3((Oracle()));
|
||||||
|
o1 = o3;
|
||||||
|
/*BOOST_CHECK(o3);
|
||||||
|
BOOST_CHECK(o3->s == sDefaultConstructed);
|
||||||
|
BOOST_CHECK(o1);
|
||||||
|
BOOST_CHECK(o1->s == sCopyConstructed);
|
||||||
|
|
||||||
|
o2 = boost::move(o3);
|
||||||
|
BOOST_CHECK(o3);
|
||||||
|
BOOST_CHECK(o3->s == sMovedFrom);
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
BOOST_CHECK(o2->s == sMoveConstructed);
|
||||||
|
|
||||||
|
o2 = optional<Oracle>((Oracle()));
|
||||||
|
BOOST_CHECK(o2);
|
||||||
|
BOOST_CHECK(o2->s == sMoveAssigned);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int test_main( int, char* [] )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||||
|
test_move_ctor_from_U();
|
||||||
|
test_move_ctor_form_T();
|
||||||
|
test_move_ctor_from_optional_T();
|
||||||
|
test_move_assign_from_U();
|
||||||
|
test_move_assign_from_T();
|
||||||
|
test_move_ssign_from_optional_T();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
catch ( ... )
|
||||||
|
{
|
||||||
|
BOOST_ERROR("Unexpected Exception caught!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user