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