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