• 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 #if defined(_MSC_VER)
10 # pragma warning( disable: 4244 ) // conversion from float to int, possible loss of data
11 #endif
12 
13 #include <boost/variant2/variant.hpp>
14 #include <boost/core/lightweight_test.hpp>
15 
16 using namespace boost::variant2;
17 
18 struct F1
19 {
operator ()F120     template<class T1> T1 operator()( T1 t1 ) const
21     {
22         return t1;
23     }
24 };
25 
26 struct F2
27 {
operator ()F228     template<class T1, class T2> auto operator()( T1 t1, T2 t2 ) const -> decltype( t1 + t2 )
29     {
30         return t1 + t2;
31     }
32 };
33 
34 struct F3
35 {
operator ()F336     template<class T1, class T2, class T3> auto operator()( T1 t1, T2 t2, T3 t3 ) const -> decltype( t1 + t2 + t3 )
37     {
38         return t1 + t2 + t3;
39     }
40 };
41 
42 struct F4
43 {
operator ()F444     template<class T1, class T2, class T3, class T4> auto operator()( T1 t1, T2 t2, T3 t3, T4 t4 ) const -> decltype( t1 + t2 + t3 + t4 )
45     {
46         return t1 + t2 + t3 + t4;
47     }
48 };
49 
main()50 int main()
51 {
52     {
53         BOOST_TEST_EQ( (visit<int>( []{ return 3.14f; } )), 3 );
54     }
55 
56     {
57         variant<int, float> v( 1 );
58 
59         BOOST_TEST_EQ( visit<int>( F1(), v ), 1 );
60         BOOST_TEST_EQ( visit<float>( F1(), v ), 1.0f );
61     }
62 
63     {
64         variant<int, float> const v( 3.14f );
65 
66         BOOST_TEST_EQ( visit<int>( F1(), v ), 3 );
67         BOOST_TEST_EQ( visit<float>( F1(), v ), 3.14f );
68     }
69 
70     {
71         variant<int, float> v1( 1 );
72         variant<int, float> const v2( 3.14f );
73 
74         BOOST_TEST_EQ( visit<int>( F2(), v1, v2 ), 4 );
75         BOOST_TEST_EQ( visit<float>( F2(), v1, v2 ), 1 + 3.14f );
76     }
77 
78     {
79         variant<int, float, double> v1( 1 );
80         variant<int, float, double> const v2( 3.14f );
81         variant<int, float, double> v3( 6.28 );
82 
83         BOOST_TEST_EQ( visit<int>( F3(), v1, v2, v3 ), 10 );
84         BOOST_TEST_EQ( visit<float>( F3(), v1, v2, v3 ), static_cast<float>( 1 + 3.14f + 6.28 ) );
85     }
86 
87     {
88         variant<int, float, double, char> v1( 1 );
89         variant<int, float, double, char> const v2( 3.14f );
90         variant<int, float, double, char> v3( 6.28 );
91         variant<int, float, double, char> const v4( 'A' );
92 
93         BOOST_TEST_EQ( visit<int>( F4(), v1, v2, v3, v4 ), 10 + 'A' );
94         BOOST_TEST_EQ( visit<float>( F4(), v1, v2, v3, v4 ), static_cast<float>( 1 + 3.14f + 6.28 + 'A' ) );
95     }
96 
97     return boost::report_errors();
98 }
99