• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct X1
19 {
20     static int instances;
21 
22     int v;
23 
X1X124     X1(): v(0) { ++instances; }
X1X125     explicit X1(int v): v(v) { ++instances; }
X1X126     X1(X1 const& r): v(r.v) { ++instances; }
X1X127     X1(X1&& r): v(r.v) { ++instances; }
28 
~X1X129     ~X1() noexcept { --instances; }
30 
operator =X131     X1& operator=( X1 const& r ) { v = r.v; return *this; }
operator =X132     X1& operator=( X1&& r ) { v = r.v; return *this; }
33 };
34 
35 int X1::instances = 0;
36 
37 struct X2
38 {
39     static int instances;
40 
41     int v;
42 
X2X243     X2(): v(0) { ++instances; }
X2X244     explicit X2(int v): v(v) { ++instances; }
X2X245     X2(X2 const& r): v(r.v) { ++instances; }
X2X246     X2(X2&& r): v(r.v) { ++instances; }
47 
~X2X248     ~X2() noexcept { --instances; }
49 
operator =X250     X2& operator=( X2 const& r ) { v = r.v; return *this; }
operator =X251     X2& operator=( X2&& r ) { v = r.v; return *this; }
52 };
53 
54 int X2::instances = 0;
55 
56 struct Y1
57 {
58     static int instances;
59 
60     int v;
61 
Y1Y162     Y1() noexcept: v(0) { ++instances; }
Y1Y163     explicit Y1(int v) noexcept: v(v) { ++instances; }
Y1Y164     Y1(Y1 const& r) noexcept: v(r.v) { ++instances; }
Y1Y165     Y1(Y1&& r) noexcept: v(r.v) { ++instances; }
66 
~Y1Y167     ~Y1() noexcept { --instances; }
68 
operator =Y169     Y1& operator=( Y1 const& r ) noexcept { v = r.v; return *this; }
operator =Y170     Y1& operator=( Y1&& r ) noexcept { v = r.v; return *this; }
71 };
72 
73 int Y1::instances = 0;
74 
75 struct Y2
76 {
77     static int instances;
78 
79     int v;
80 
Y2Y281     Y2() noexcept: v(0) { ++instances; }
Y2Y282     explicit Y2(int v) noexcept: v(v) { ++instances; }
Y2Y283     Y2(Y2 const& r) noexcept: v(r.v) { ++instances; }
Y2Y284     Y2(Y2&& r) noexcept: v(r.v) { ++instances; }
85 
~Y2Y286     ~Y2() noexcept { --instances; }
87 
operator =Y288     Y2& operator=( Y2 const& r ) noexcept { v = r.v; return *this; }
operator =Y289     Y2& operator=( Y2&& r ) noexcept { v = r.v; return *this; }
90 };
91 
92 int Y2::instances = 0;
93 
main()94 int main()
95 {
96     BOOST_TEST_EQ( Y1::instances, 0 );
97 
98     {
99         variant<Y1> v;
100         BOOST_TEST_EQ( Y1::instances, 1 );
101 
102         {
103             variant<Y1> v2;
104             BOOST_TEST_EQ( Y1::instances, 2 );
105 
106             v = v2;
107             BOOST_TEST_EQ( Y1::instances, 2 );
108         }
109 
110         BOOST_TEST_EQ( Y1::instances, 1 );
111 
112         v = Y1{1};
113         BOOST_TEST_EQ( Y1::instances, 1 );
114     }
115 
116     BOOST_TEST_EQ( Y1::instances, 0 );
117     BOOST_TEST_EQ( Y2::instances, 0 );
118 
119     {
120         variant<Y1, Y2> v;
121 
122         BOOST_TEST_EQ( Y1::instances, 1 );
123         BOOST_TEST_EQ( Y2::instances, 0 );
124 
125         {
126             variant<Y1, Y2> v2;
127             BOOST_TEST_EQ( Y1::instances, 2 );
128             BOOST_TEST_EQ( Y2::instances, 0 );
129 
130             v = v2;
131             BOOST_TEST_EQ( Y1::instances, 2 );
132             BOOST_TEST_EQ( Y2::instances, 0 );
133 
134             v2 = Y2{1};
135             BOOST_TEST_EQ( Y1::instances, 1 );
136             BOOST_TEST_EQ( Y2::instances, 1 );
137 
138             v = v2;
139             BOOST_TEST_EQ( Y1::instances, 0 );
140             BOOST_TEST_EQ( Y2::instances, 2 );
141         }
142 
143         BOOST_TEST_EQ( Y1::instances, 0 );
144         BOOST_TEST_EQ( Y2::instances, 1 );
145 
146         v.emplace<0>();
147 
148         BOOST_TEST_EQ( Y1::instances, 1 );
149         BOOST_TEST_EQ( Y2::instances, 0 );
150 
151         v.emplace<Y2>();
152 
153         BOOST_TEST_EQ( Y1::instances, 0 );
154         BOOST_TEST_EQ( Y2::instances, 1 );
155     }
156 
157     BOOST_TEST_EQ( Y1::instances, 0 );
158     BOOST_TEST_EQ( Y2::instances, 0 );
159 
160     BOOST_TEST_EQ( X1::instances, 0 );
161     BOOST_TEST_EQ( X2::instances, 0 );
162 
163     {
164         variant<X1, X2> v;
165 
166         BOOST_TEST_EQ( X1::instances, 1 );
167         BOOST_TEST_EQ( X2::instances, 0 );
168 
169         {
170             variant<X1, X2> v2;
171             BOOST_TEST_EQ( X1::instances, 2 );
172             BOOST_TEST_EQ( X2::instances, 0 );
173 
174             v = v2;
175             BOOST_TEST_EQ( X1::instances, 2 );
176             BOOST_TEST_EQ( X2::instances, 0 );
177 
178             v2 = X2{1};
179             BOOST_TEST_EQ( X1::instances, 1 );
180             BOOST_TEST_EQ( X2::instances, 1 );
181 
182             v = v2;
183             BOOST_TEST_EQ( X1::instances, 0 );
184             BOOST_TEST_EQ( X2::instances, 2 );
185         }
186 
187         BOOST_TEST_EQ( X1::instances, 0 );
188         BOOST_TEST_EQ( X2::instances, 1 );
189 
190         v.emplace<0>();
191 
192         BOOST_TEST_EQ( X1::instances, 1 );
193         BOOST_TEST_EQ( X2::instances, 0 );
194 
195         v.emplace<X2>();
196 
197         BOOST_TEST_EQ( X1::instances, 0 );
198         BOOST_TEST_EQ( X2::instances, 1 );
199     }
200 
201     BOOST_TEST_EQ( X1::instances, 0 );
202     BOOST_TEST_EQ( X2::instances, 0 );
203 
204     return boost::report_errors();
205 }
206