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 {
22 int v;
23
X1X124 X1(): v(0) {}
X1X125 explicit X1(int v): v(v) {}
X1X126 X1(X1 const& r): v(r.v) {}
X1X127 X1(X1&& r): v(r.v) {}
operator =X128 X1& operator=( X1 const& r ) { v = r.v; return *this; }
operator =X129 X1& operator=( X1&& r ) { v = r.v; return *this; }
30 };
31
operator ==(X1 const & a,X1 const & b)32 inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
33
34 STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
35 STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
36 STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
37 STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
38 STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
39
40 struct X2
41 {
42 int v;
43
X2X244 X2(): v(0) {}
X2X245 explicit X2(int v): v(v) {}
X2X246 X2(X2 const& r): v(r.v) {}
X2X247 X2(X2&& r): v(r.v) {}
operator =X248 X2& operator=( X2 const& r ) { v = r.v; return *this; }
operator =X249 X2& operator=( X2&& r ) { v = r.v; return *this; }
50 };
51
operator ==(X2 const & a,X2 const & b)52 inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
53
54 STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
55 STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
56 STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
57 STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
58 STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
59
60 struct Y
61 {
62 Y& operator=( Y&& ) = delete;
63 };
64
main()65 int main()
66 {
67 {
68 variant<int> v;
69 BOOST_TEST_EQ( get<0>(v), 0 );
70
71 variant<int> v2( 1 );
72
73 v = std::move(v2);
74 BOOST_TEST_EQ( get<0>(v), 1 );
75
76 variant<int> v3( 2 );
77
78 v = std::move(v3);
79 BOOST_TEST_EQ( get<0>(v), 2 );
80 }
81
82 {
83 variant<int, float> v;
84 BOOST_TEST_EQ( v.index(), 0 );
85 BOOST_TEST_EQ( get<0>(v), 0 );
86
87 variant<int, float> v2( 1 );
88
89 v = std::move(v2);
90 BOOST_TEST_EQ( v.index(), 0 );
91 BOOST_TEST_EQ( get<0>(v), 1 );
92
93 variant<int, float> v3( 3.14f );
94
95 v = std::move(v3);
96 BOOST_TEST_EQ( v.index(), 1 );
97 BOOST_TEST_EQ( get<1>(v), 3.14f );
98
99 variant<int, float> v4( 3.15f );
100
101 v = std::move(v4);
102 BOOST_TEST_EQ( v.index(), 1 );
103 BOOST_TEST_EQ( get<1>(v), 3.15f );
104 }
105
106 {
107 variant<int, int, float, std::string> v;
108 BOOST_TEST_EQ( v.index(), 0 );
109 BOOST_TEST_EQ( get<0>(v), 0 );
110
111 variant<int, int, float, std::string> v2( in_place_index_t<1>{}, 1 );
112
113 v = std::move(v2);
114 BOOST_TEST_EQ( v.index(), 1 );
115 BOOST_TEST_EQ( get<1>(v), 1 );
116
117 variant<int, int, float, std::string> v3( 3.14f );
118
119 v = std::move(v3);
120 BOOST_TEST_EQ( v.index(), 2 );
121 BOOST_TEST_EQ( get<2>(v), 3.14f );
122
123 variant<int, int, float, std::string> v4( 3.15f );
124
125 v = std::move(v4);
126 BOOST_TEST_EQ( v.index(), 2 );
127 BOOST_TEST_EQ( get<2>(v), 3.15f );
128
129 variant<int, int, float, std::string> v5( "s1" );
130
131 v = std::move(v5);
132 BOOST_TEST_EQ( v.index(), 3 );
133 BOOST_TEST_EQ( get<3>(v), std::string("s1") );
134
135 variant<int, int, float, std::string> v6( "s2" );
136
137 v = std::move(v6);
138 BOOST_TEST_EQ( v.index(), 3 );
139 BOOST_TEST_EQ( get<3>(v), std::string("s2") );
140 }
141
142 {
143 variant<X1, X2> v;
144 BOOST_TEST_EQ( v.index(), 0 );
145 BOOST_TEST_EQ( get<0>(v).v, 0 );
146
147 variant<X1, X2> v2( X1{1} );
148
149 v = std::move(v2);
150 BOOST_TEST_EQ( v.index(), 0 );
151 BOOST_TEST_EQ( get<0>(v).v, 1 );
152
153 variant<X1, X2> v3( in_place_index_t<1>{}, 2 );
154
155 v = std::move(v3);
156 BOOST_TEST_EQ( v.index(), 1 );
157 BOOST_TEST_EQ( get<1>(v).v, 2 );
158
159 variant<X1, X2> v4( in_place_index_t<1>{}, 3 );
160
161 v = std::move(v4);
162 BOOST_TEST_EQ( v.index(), 1 );
163 BOOST_TEST_EQ( get<1>(v).v, 3 );
164
165 variant<X1, X2> v5( in_place_index_t<0>{}, 4 );
166
167 v = std::move(v5);
168 BOOST_TEST_EQ( v.index(), 0 );
169 BOOST_TEST_EQ( get<0>(v).v, 4 );
170 }
171
172 {
173 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int>>));
174 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, int>>));
175 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, float>>));
176 BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, int, float, float>>));
177
178 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1>>));
179 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, int>>));
180 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, int, float>>));
181
182 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<int, X1>>));
183 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<int, int, X1>>));
184
185 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, X2>>));
186 BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, X2, int, int>>));
187
188 BOOST_TEST_TRAIT_TRUE((std::is_move_assignable<variant<X1, X2>>));
189 BOOST_TEST_TRAIT_FALSE((std::is_move_assignable<variant<int const>>));
190 BOOST_TEST_TRAIT_FALSE((std::is_move_assignable<variant<int, float, Y>>));
191 }
192
193 return boost::report_errors();
194 }
195