1 
2 // Copyright 2017 Peter Dimov.
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 //
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 
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>
13 #include <utility>
14 #include <string>
15 
16 using namespace boost::variant2;
17 
18 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
19 
20 struct X1
21 {
X1X122     X1() {}
X1X123     X1(X1 const&) {}
X1X124     X1(X1&&) {}
25 };
26 
operator ==(X1,X1)27 inline bool operator==( X1, X1 ) { return true; }
28 
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 );
32 
33 struct X2
34 {
X2X235     X2() {}
X2X236     X2(X2 const&) {}
X2X237     X2(X2&&) {}
38 };
39 
operator ==(X2,X2)40 inline bool operator==( X2, X2 ) { return true; }
41 
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 );
45 
46 struct Y
47 {
48     Y( Y&& ) = delete;
49 };
50 
test(V && v)51 template<class V> static void test( V&& v )
52 {
53     V v2( v );
54     V v3( std::move(v) );
55 
56     BOOST_TEST_EQ( v2.index(), v3.index() );
57     BOOST_TEST( v2 == v3 );
58 }
59 
main()60 int main()
61 {
62     test( variant<int>() );
63     test( variant<int>(1) );
64 
65     test( variant<int const>() );
66     test( variant<int const>(1) );
67 
68     test( variant<int, float>() );
69     test( variant<int, float>(1) );
70     test( variant<int, float>(3.14f) );
71 
72     test( variant<int const, float const>() );
73     test( variant<int const, float const>(1) );
74     test( variant<int const, float const>(3.14f) );
75 
76     test( variant<std::string>() );
77     test( variant<std::string>("test") );
78 
79     test( variant<std::string const>() );
80     test( variant<std::string const>("test") );
81 
82     test( variant<int, float, std::string>() );
83     test( variant<int, float, std::string>(1) );
84     test( variant<int, float, std::string>(3.14f) );
85     test( variant<int, float, std::string>("test") );
86 
87     test( variant<int, int>() );
88 
89     test( variant<int, int, float>() );
90     test( variant<int, int, float>(3.14f) );
91 
92     test( variant<int, int, float, float>() );
93 
94     test( variant<int, int, float, float, std::string>("test") );
95 
96     test( variant<std::string, std::string, float>() );
97 
98     test( variant<X1 const>() );
99 
100     test( variant<X1, X2>() );
101     test( variant<X1, X2, int>() );
102     test( variant<X1, X2, X2>() );
103     test( variant<X1, X1, X2, X2>() );
104 
105     {
106         variant<X1, X2> v;
107         v.emplace<X2>();
108 
109         test( std::move(v) );
110     }
111 
112     {
113         variant<X1, X1, X2> v;
114         v.emplace<X2>();
115 
116         test( std::move(v) );
117     }
118 
119     {
120         BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int>>));
121         BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int const>>));
122         BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int, int>>));
123         BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int, float>>));
124         BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_constructible<variant<int, int, float, float>>));
125 
126         BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1>>));
127         BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, int>>));
128         BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, int, float>>));
129 
130         BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<int, X1>>));
131         BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<int, int, X1>>));
132 
133         BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, X2>>));
134         BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_constructible<variant<X1, X2, int, int>>));
135 
136         BOOST_TEST_TRAIT_TRUE((std::is_move_constructible<variant<X1, X2>>));
137 
138 #if !BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
139 
140         BOOST_TEST_TRAIT_FALSE((std::is_move_constructible<variant<int, float, Y>>));
141 
142 #endif
143     }
144 
145     return boost::report_errors();
146 }
147