1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
// 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( 1 );
variant<int, float> v2( v );
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v ), get<int>( v2 ) );
variant<int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
}
{
variant<int> const v( 1 );
variant<int, float> v2( v );
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v ), get<int>( v2 ) );
variant<int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
}
{
variant<int const> v( 1 );
variant<int const, float> v2( v );
BOOST_TEST( holds_alternative<int const>( v2 ) );
BOOST_TEST_EQ( get<int const>( v ), get<int const>( v2 ) );
variant<int const, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int const>( v3 ) );
BOOST_TEST_EQ( get<int const>( v2 ), get<int const>( v3 ) );
}
{
variant<int const> const v( 1 );
variant<int const, float> v2( v );
BOOST_TEST( holds_alternative<int const>( v2 ) );
BOOST_TEST_EQ( get<int const>( v ), get<int const>( v2 ) );
variant<int const, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int const>( v3 ) );
BOOST_TEST_EQ( get<int const>( v2 ), get<int const>( v3 ) );
}
{
variant<float> v( 3.14f );
variant<int, float> v2( v );
BOOST_TEST( holds_alternative<float>( v2 ) );
BOOST_TEST_EQ( get<float>( v ), get<float>( v2 ) );
variant<int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<float>( v3 ) );
BOOST_TEST_EQ( get<float>( v2 ), get<float>( v3 ) );
}
{
variant<float> v( 3.15f );
variant<int, int, float> v2( v );
BOOST_TEST( holds_alternative<float>( v2 ) );
BOOST_TEST_EQ( get<float>( v ), get<float>( v2 ) );
variant<int, int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<float>( v3 ) );
BOOST_TEST_EQ( get<float>( v2 ), get<float>( v3 ) );
}
{
variant<float, std::string> v( "s1" );
variant<int, int, float, std::string> v2( v );
BOOST_TEST( holds_alternative<std::string>( v2 ) );
BOOST_TEST_EQ( get<std::string>( v ), get<std::string>( v2 ) );
variant<int, int, float, std::string> v3( std::move(v) );
BOOST_TEST( holds_alternative<std::string>( v3 ) );
BOOST_TEST_EQ( get<std::string>( v2 ), get<std::string>( v3 ) );
}
{
variant<X1, X2> v{ X1{1} };
variant<int, int, float, float, X1, X2> v2( v );
BOOST_TEST( holds_alternative<X1>( v2 ) );
BOOST_TEST_EQ( get<X1>( v ).v, get<X1>( v2 ).v );
variant<int, int, float, float, X1, X2> v3( std::move(v) );
BOOST_TEST( holds_alternative<X1>( v3 ) );
BOOST_TEST_EQ( get<X1>( v2 ).v, get<X1>( v3 ).v );
}
return boost::report_errors();
}
|