• 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 <boost/config.hpp>
11 
12 using namespace boost::variant2;
13 
14 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
15 
16 #if BOOST_WORKAROUND(BOOST_GCC, < 50000)
17 # define STATIC_ASSERT_IF(...)
18 #else
19 # define STATIC_ASSERT_IF(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
20 #endif
21 
main()22 int main()
23 {
24     {
25         constexpr variant<int> v;
26 
27         STATIC_ASSERT( get<0>(v) == 0 );
28 
29         STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) );
30     }
31 
32     {
33         constexpr variant<int> v( 1 );
34 
35         STATIC_ASSERT( get<0>(v) == 1 );
36 
37         STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) );
38     }
39 
40     {
41         constexpr variant<int, float> v;
42 
43         STATIC_ASSERT( get<0>(v) == 0 );
44 
45         STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) );
46         STATIC_ASSERT_IF( get_if<1>(&v) == nullptr );
47     }
48 
49     {
50         constexpr variant<int, float> v( 1 );
51 
52         STATIC_ASSERT( get<0>(v) == 1 );
53 
54         STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) );
55         STATIC_ASSERT_IF( get_if<1>(&v) == nullptr );
56     }
57 
58     {
59         constexpr variant<int, float> v( 3.14f );
60 
61         STATIC_ASSERT( get<1>(v) == 3.14f );
62 
63         STATIC_ASSERT_IF( get_if<0>(&v) == nullptr );
64         STATIC_ASSERT_IF( get_if<1>(&v) == &get<1>(v) );
65     }
66 
67     {
68         constexpr variant<int, float, float> v;
69 
70         STATIC_ASSERT( get<0>(v) == 0 );
71 
72         STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) );
73         STATIC_ASSERT_IF( get_if<1>(&v) == nullptr );
74         STATIC_ASSERT_IF( get_if<2>(&v) == nullptr );
75     }
76 
77     {
78         constexpr variant<int, float, float> v( 1 );
79 
80         STATIC_ASSERT( get<0>(v) == 1 );
81 
82         STATIC_ASSERT_IF( get_if<0>(&v) == &get<0>(v) );
83         STATIC_ASSERT_IF( get_if<1>(&v) == nullptr );
84         STATIC_ASSERT_IF( get_if<2>(&v) == nullptr );
85     }
86 
87     {
88         constexpr variant<int, int, float> v( 3.14f );
89 
90         STATIC_ASSERT( get<2>(v) == 3.14f );
91 
92         STATIC_ASSERT_IF( get_if<0>(&v) == nullptr );
93         STATIC_ASSERT_IF( get_if<1>(&v) == nullptr );
94         STATIC_ASSERT_IF( get_if<2>(&v) == &get<2>(v) );
95     }
96 }
97