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