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 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 Y( int v ): v( v ) {}
operator intY26     constexpr operator int() const { return v; }
27 };
28 
29 enum E
30 {
31     v
32 };
33 
34 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
35 
test(A const & a)36 template<class V, class T, class A> constexpr A test( A const& a )
37 {
38     V v;
39 
40     v = a;
41 
42     return get<T>(v);
43 }
44 
main()45 int main()
46 {
47     {
48         constexpr auto w = test<variant<int>, int>( 1 );
49         STATIC_ASSERT( w == 1 );
50     }
51 
52     {
53         constexpr auto w = test<variant<X>, X>( 1 );
54         STATIC_ASSERT( w == 1 );
55     }
56 
57 #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
58 #else
59 
60     {
61         constexpr auto w = test<variant<Y>, Y>( 1 );
62         STATIC_ASSERT( w == 1 );
63     }
64 
65 #endif
66 
67     {
68         constexpr auto w = test<variant<int, float>, int>( 1 );
69         STATIC_ASSERT( w == 1 );
70     }
71 
72     {
73         constexpr auto w = test<variant<int, float>, float>( 3.0f );
74         STATIC_ASSERT( w == 3.0f );
75     }
76 
77     {
78         constexpr auto w = test<variant<int, int, float>, float>( 3.0f );
79         STATIC_ASSERT( w == 3.0f );
80     }
81 
82     {
83         constexpr auto w = test<variant<E, E, X>, X>( 1 );
84         STATIC_ASSERT( w == 1 );
85     }
86 
87     {
88         constexpr auto w = test<variant<int, int, float, float, X>, X>( X(1) );
89         STATIC_ASSERT( w == 1 );
90     }
91 
92 #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
93 #else
94 
95     {
96         constexpr auto w = test<variant<E, E, Y>, Y>( 1 );
97         STATIC_ASSERT( w == 1 );
98     }
99 
100     {
101         constexpr auto w = test<variant<int, int, float, float, Y>, Y>( Y(1) );
102         STATIC_ASSERT( w == 1 );
103     }
104 
105 #endif
106 }
107