• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2017, 2020 Peter Dimov.
3 // Distributed under the Boost Software License, Version 1.0.
4 // https://www.boost.org/LICENSE_1_0.txt
5 
6 #if defined(_MSC_VER)
7 # pragma warning( disable: 4244 ) // conversion from float to int, possible loss of data
8 #endif
9 
10 #include <boost/variant2/variant.hpp>
11 #include <boost/core/lightweight_test.hpp>
12 #include <boost/config.hpp>
13 #include <boost/config/workaround.hpp>
14 #include <utility>
15 
16 struct X: boost::variant2::variant<int, float>
17 {
18 #if BOOST_WORKAROUND( BOOST_MSVC, < 1930 )
19 
XX20     template<class T> explicit X( T&& t ): variant( std::forward<T>( t ) ) {};
21 
22 #else
23 
24     using variant::variant;
25 
26 #endif
27 };
28 
29 template<class... T> struct Y: boost::variant2::variant<T...>
30 {
31     using boost::variant2::variant<T...>::variant;
32 };
33 
main()34 int main()
35 {
36     {
37         X v1( 1 );
38         X const v2( 3.14f );
39 
40         BOOST_TEST_EQ( (visit( []( int x1, float x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 );
41 
42         visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 );
43         visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, std::move(v1), std::move(v2) );
44     }
45 
46     {
47         Y<int, float> v1( 1 );
48         Y<int, float> const v2( 3.14f );
49 
50         BOOST_TEST_EQ( (visit( []( int x1, float x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 );
51 
52         visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 );
53         visit( []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, std::move(v1), std::move(v2) );
54     }
55 
56     return boost::report_errors();
57 }
58