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