2 // Copyright 2017 Peter Dimov.
4 // Distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
9 #include <boost/variant2/variant.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <boost/core/lightweight_test_trait.hpp>
12 #include <type_traits>
16 using namespace boost::variant2
;
18 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
27 inline bool operator==( X1
, X1
) { return true; }
29 STATIC_ASSERT( !std::is_nothrow_default_constructible
<X1
>::value
);
30 STATIC_ASSERT( !std::is_nothrow_copy_constructible
<X1
>::value
);
31 STATIC_ASSERT( !std::is_nothrow_move_constructible
<X1
>::value
);
40 inline bool operator==( X2
, X2
) { return true; }
42 STATIC_ASSERT( !std::is_nothrow_default_constructible
<X2
>::value
);
43 STATIC_ASSERT( !std::is_nothrow_copy_constructible
<X2
>::value
);
44 STATIC_ASSERT( !std::is_nothrow_move_constructible
<X2
>::value
);
48 Y( Y
const& ) = delete;
51 template<class V
> static void test( V
const & v
)
55 BOOST_TEST_EQ( v
.index(), v2
.index() );
56 BOOST_TEST( v
== v2
);
61 test( variant
<int>() );
62 test( variant
<int>(1) );
64 test( variant
<int const>() );
65 test( variant
<int const>(1) );
67 test( variant
<int, float>() );
68 test( variant
<int, float>(1) );
69 test( variant
<int, float>(3.14f
) );
71 test( variant
<int const, float const>() );
72 test( variant
<int const, float const>(1) );
73 test( variant
<int const, float const>(3.14f
) );
75 test( variant
<std::string
>() );
76 test( variant
<std::string
>("test") );
78 test( variant
<std::string
const>() );
79 test( variant
<std::string
const>("test") );
81 test( variant
<int, float, std::string
>() );
82 test( variant
<int, float, std::string
>(1) );
83 test( variant
<int, float, std::string
>(3.14f
) );
84 test( variant
<int, float, std::string
>("test") );
86 test( variant
<int, int>() );
88 test( variant
<int, int, float>() );
89 test( variant
<int, int, float>(3.14f
) );
91 test( variant
<int, int, float, float>() );
93 test( variant
<int, int, float, float, std::string
>("test") );
95 test( variant
<std::string
, std::string
, float>() );
97 test( variant
<X1
const>() );
99 test( variant
<X1
, X2
>() );
100 test( variant
<X1
, X2
, int>() );
101 test( variant
<X1
, X2
, X2
>() );
102 test( variant
<X1
, X1
, X2
, X2
>() );
112 variant
<X1
, X1
, X2
> v
;
119 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible
<variant
<int>>));
120 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible
<variant
<int const>>));
121 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible
<variant
<int, int>>));
122 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible
<variant
<int, float>>));
123 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_copy_constructible
<variant
<int, int, float, float>>));
125 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible
<variant
<X1
>>));
126 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible
<variant
<X1
, int>>));
127 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible
<variant
<X1
, int, float>>));
129 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible
<variant
<int, X1
>>));
130 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible
<variant
<int, int, X1
>>));
132 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible
<variant
<X1
, X2
>>));
133 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_copy_constructible
<variant
<X1
, X2
, int, int>>));
135 BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible
<variant
<X1
, X2
>>));
137 #if !BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
139 BOOST_TEST_TRAIT_FALSE((std::is_copy_constructible
<variant
<int, float, Y
>>));
144 return boost::report_errors();