Add test/variant_copy_assign, variant_move_assign

This commit is contained in:
Peter Dimov
2017-05-30 18:00:07 +03:00
parent 21acb2bda0
commit e0e48d365c
5 changed files with 386 additions and 12 deletions

View File

@ -114,25 +114,37 @@ template<class U, class... T> constexpr bool holds_alternative( variant<T...> co
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& get(variant<T...>& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
if( v.index() != I ) throw bad_variant_access();
return v._get_impl( mp_size_t<I>() );
}
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>&& get(variant<T...>&& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
if( v.index() != I ) throw bad_variant_access();
return std::move( v._get_impl( mp_size_t<I>() ) );
}
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...> const>& get(variant<T...> const& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
if( v.index() != I ) throw bad_variant_access();
return v._get_impl( mp_size_t<I>() );
}
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...> const>&& get(variant<T...> const&& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
if( v.index() != I ) throw bad_variant_access();
return std::move( v._get_impl( mp_size_t<I>() ) );
}
// get
@ -142,7 +154,9 @@ template<class U, class... T> constexpr U& get(variant<T...>& v)
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
if( v.index() != I ) throw bad_variant_access();
return v._get_impl( mp_size_t<I>() );
}
template<class U, class... T> constexpr U&& get(variant<T...>&& v)
@ -150,7 +164,9 @@ template<class U, class... T> constexpr U&& get(variant<T...>&& v)
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
if( v.index() != I ) throw bad_variant_access();
return std::move( v._get_impl( mp_size_t<I>() ) );
}
template<class U, class... T> constexpr U const& get(variant<T...> const& v)
@ -158,7 +174,9 @@ template<class U, class... T> constexpr U const& get(variant<T...> const& v)
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
if( v.index() != I ) throw bad_variant_access();
return v._get_impl( mp_size_t<I>() );
}
template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
@ -166,7 +184,9 @@ template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
if( v.index() != I ) throw bad_variant_access();
return std::move( v._get_impl( mp_size_t<I>() ) );
}
// get_if
@ -699,13 +719,13 @@ public:
if( J == r.index() )
{
if( index() == J )
if( this->index() == J )
{
get<J>(*this) = get<J>(r);
}
else
{
variant_base::template emplace<J>( get<J>(r) );
this->variant_base::template emplace<J>( get<J>(r) );
}
}
@ -722,13 +742,13 @@ public:
if( J == r.index() )
{
if( index() == J )
if( this->index() == J )
{
get<J>(*this) = get<J>(std::move(r));
}
else
{
variant_base::template emplace<J>( get<J>(std::move(r)) );
this->variant_base::template emplace<J>( get<J>(std::move(r)) );
}
}

View File

@ -29,3 +29,5 @@ run variant_in_place_index_construct.cpp : : : $(REQ) ;
compile variant_in_place_index_construct_cx.cpp : : : $(REQ) ;
run variant_in_place_type_construct.cpp : : : $(REQ) ;
compile variant_in_place_type_construct_cx.cpp : : : $(REQ) ;
run variant_copy_assign.cpp : : : $(REQ) ;
run variant_move_assign.cpp : : : $(REQ) ;

View File

@ -54,6 +54,8 @@ int main()
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_EQ( get<0>(std::move(v)), 0 );
}
{
@ -61,6 +63,8 @@ int main()
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_EQ( get<0>(std::move(v)), 1 );
}
{
@ -119,6 +123,8 @@ int main()
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 0 );
}
{
@ -129,6 +135,8 @@ int main()
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 1 );
}
{
@ -139,6 +147,8 @@ int main()
BOOST_TEST_EQ( get<1>(v), 3.14f );
BOOST_TEST_EQ( get_if<1>(&v), &get<1>(v) );
BOOST_TEST_EQ( get<1>(std::move(v)), 3.14f );
}
{
@ -152,6 +162,8 @@ int main()
BOOST_TEST_THROWS( get<2>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<2>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 0 );
}
{
@ -165,6 +177,8 @@ int main()
BOOST_TEST_THROWS( get<2>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<2>(&v), nullptr );
BOOST_TEST_EQ( get<0>(std::move(v)), 1 );
}
{
@ -178,6 +192,8 @@ int main()
BOOST_TEST_EQ( get<2>(v), 3.14f );
BOOST_TEST_EQ( get_if<2>(&v), &get<2>(v) );
BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f );
}
return boost::report_errors();

View File

@ -0,0 +1,168 @@
// Copyright 2017 Peter Dimov.
//
// 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
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int> v2( 1 );
v = v2;
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int> const v3( 2 );
v = v3;
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, float> v2( 1 );
v = v2;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int, float> v3( 3.14f );
v = v3;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
variant<int, float> const v4( 3.15f );
v = v4;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.15f );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, int, float, std::string> v2( in_place_index<1>, 1 );
v = v2;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
variant<int, int, float, std::string> v3( 3.14f );
v = v3;
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
variant<int, int, float, std::string> const v4( 3.15f );
v = v4;
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.15f );
variant<int, int, float, std::string> v5( "s1" );
v = v5;
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
variant<int, int, float, std::string> const v6( "s2" );
v = v6;
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
variant<X1, X2> v2( X1{1} );
v = v2;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
variant<X1, X2> v3( in_place_index<1>, 2 );
v = v3;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
variant<X1, X2> const v4( in_place_index<1>, 3 );
v = v4;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 3 );
variant<X1, X2> const v5( in_place_index<0>, 4 );
v = v5;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
return boost::report_errors();
}

View File

@ -0,0 +1,168 @@
// Copyright 2017 Peter Dimov.
//
// 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
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int> v2( 1 );
v = std::move(v2);
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int> v3( 2 );
v = std::move(v3);
BOOST_TEST_EQ( get<0>(v), 2 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, float> v2( 1 );
v = std::move(v2);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
variant<int, float> v3( 3.14f );
v = std::move(v3);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
variant<int, float> v4( 3.15f );
v = std::move(v4);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.15f );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
variant<int, int, float, std::string> v2( in_place_index<1>, 1 );
v = std::move(v2);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
variant<int, int, float, std::string> v3( 3.14f );
v = std::move(v3);
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
variant<int, int, float, std::string> v4( 3.15f );
v = std::move(v4);
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.15f );
variant<int, int, float, std::string> v5( "s1" );
v = std::move(v5);
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
variant<int, int, float, std::string> v6( "s2" );
v = std::move(v6);
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
variant<X1, X2> v2( X1{1} );
v = std::move(v2);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
variant<X1, X2> v3( in_place_index<1>, 2 );
v = std::move(v3);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
variant<X1, X2> v4( in_place_index<1>, 3 );
v = std::move(v4);
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 3 );
variant<X1, X2> v5( in_place_index<0>, 4 );
v = std::move(v5);
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
return boost::report_errors();
}