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 
11 using namespace boost::variant2;
12 
13 struct X
14 {
15     int v;
16     X() = default;
XX17     constexpr explicit X( int v ): v( v ) {}
operator intX18     constexpr operator int() const { return v; }
19 };
20 
21 struct Y
22 {
23     int v;
YY24     constexpr Y(): v() {}
YY25     constexpr explicit Y( int v ): v( v ) {}
operator intY26     constexpr operator int() const { return v; }
27 };
28 
29 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
30 
test(A const & a)31 template<class V, std::size_t I, class A> constexpr A test( A const& a )
32 {
33     V v;
34 
35     v.template emplace<I>( a );
36 
37     return get<I>(v);
38 }
39 
main()40 int main()
41 {
42     {
43         constexpr auto w = test<variant<int>, 0>( 1 );
44         STATIC_ASSERT( w == 1 );
45     }
46 
47     {
48         constexpr auto w = test<variant<X>, 0>( 1 );
49         STATIC_ASSERT( w == 1 );
50     }
51 
52 #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
53 #else
54 
55     {
56         constexpr auto w = test<variant<Y>, 0>( 1 );
57         STATIC_ASSERT( w == 1 );
58     }
59 
60 #endif
61 
62     {
63         constexpr auto w = test<variant<int, float>, 0>( 1 );
64         STATIC_ASSERT( w == 1 );
65     }
66 
67     {
68         constexpr auto w = test<variant<int, float>, 1>( 3.0f );
69         STATIC_ASSERT( w == 3.0f );
70     }
71 
72     {
73         constexpr auto w = test<variant<int, int, float, float, X, X>, 0>( 1 );
74         STATIC_ASSERT( w == 1 );
75     }
76 
77     {
78         constexpr auto w = test<variant<int, int, float, float, X, X>, 1>( 1 );
79         STATIC_ASSERT( w == 1 );
80     }
81 
82     {
83         constexpr auto w = test<variant<int, int, float, float, X, X>, 2>( 2.0f );
84         STATIC_ASSERT( w == 2.0f );
85     }
86 
87     {
88         constexpr auto w = test<variant<int, int, float, float, X, X>, 3>( 3.0f );
89         STATIC_ASSERT( w == 3.0f );
90     }
91 
92     {
93         constexpr auto w = test<variant<int, int, float, float, X, X>, 4>( 4 );
94         STATIC_ASSERT( w == 4 );
95     }
96 
97     {
98         constexpr auto w = test<variant<int, int, float, float, X, X>, 5>( 5 );
99         STATIC_ASSERT( w == 5 );
100     }
101 
102 #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
103 #else
104 
105     {
106         constexpr auto w = test<variant<int, int, float, float, Y, Y>, 0>( 1 );
107         STATIC_ASSERT( w == 1 );
108     }
109 
110     {
111         constexpr auto w = test<variant<int, int, float, float, Y, Y>, 1>( 1 );
112         STATIC_ASSERT( w == 1 );
113     }
114 
115     {
116         constexpr auto w = test<variant<int, int, float, float, Y, Y>, 2>( 2.0f );
117         STATIC_ASSERT( w == 2.0f );
118     }
119 
120     {
121         constexpr auto w = test<variant<int, int, float, float, Y, Y>, 3>( 3.0f );
122         STATIC_ASSERT( w == 3.0f );
123     }
124 
125     {
126         constexpr auto w = test<variant<int, int, float, float, Y, Y>, 4>( 4 );
127         STATIC_ASSERT( w == 4 );
128     }
129 
130     {
131         constexpr auto w = test<variant<int, int, float, float, Y, Y>, 5>( 5 );
132         STATIC_ASSERT( w == 5 );
133     }
134 
135 #endif
136 }
137