1 2 // Copyright 2019 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 #if defined(_MSC_VER) 10 # pragma warning( disable: 4702 ) // unreachable code 11 #endif 12 13 #include <boost/variant2/variant.hpp> 14 #include <boost/core/lightweight_test.hpp> 15 #include <stdexcept> 16 17 using namespace boost::variant2; 18 19 struct X 20 { 21 static int instances; 22 XX23 X() 24 { 25 ++instances; 26 } 27 XX28 X( X const& ) 29 { 30 throw std::runtime_error( "X(X const&)" ); 31 } 32 ~XX33 ~X() 34 { 35 --instances; 36 } 37 }; 38 39 int X::instances = 0; 40 test()41void test() 42 { 43 X::instances = 0; 44 45 { 46 variant<X> v1; 47 48 BOOST_TEST_EQ( X::instances, 1 ); 49 50 try 51 { 52 variant<X> v2( std::move( v1 ) ); 53 BOOST_TEST_EQ( X::instances, 2 ); 54 } 55 catch( std::exception const& ) 56 { 57 } 58 59 BOOST_TEST_EQ( X::instances, 1 ); 60 } 61 62 BOOST_TEST_EQ( X::instances, 0 ); 63 } 64 main()65int main() 66 { 67 test(); 68 return boost::report_errors(); 69 } 70