1 | #include <cstdio> |
2 | #include <string> |
3 | #include <vector> |
4 | |
5 | // If we have libc++ 4.0 or greater we should have <variant> |
6 | // According to libc++ C++1z status page https://libcxx.llvm.org/cxx1z_status.html |
7 | #if _LIBCPP_VERSION >= 4000 |
8 | #include <variant> |
9 | #define HAVE_VARIANT 1 |
10 | #else |
11 | #define HAVE_VARIANT 0 |
12 | #endif |
13 | |
14 | struct S { |
15 | operator int() { throw 42; } |
16 | } ; |
17 | |
18 | |
19 | int main() |
20 | { |
21 | bool has_variant = HAVE_VARIANT ; |
22 | |
23 | printf( format: "%d\n" , has_variant ) ; // break here |
24 | |
25 | #if HAVE_VARIANT == 1 |
26 | std::variant<int, double, char> v1; |
27 | std::variant<int, double, char> &v1_ref = v1; |
28 | std::variant<int, double, char> v2; |
29 | std::variant<int, double, char> v3; |
30 | std::variant<std::variant<int,double,char>> v_v1 ; |
31 | std::variant<int, double, char> v_no_value; |
32 | // The next variant has 300 types, meaning the type index does not fit in |
33 | // a byte and must be `unsigned short` instead of `unsigned char` when |
34 | // using the unstable libc++ ABI. With stable libc++ ABI, the type index |
35 | // is always just `unsigned int`. |
36 | std::variant< |
37 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
38 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
39 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
40 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
41 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
42 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
43 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
44 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
45 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
46 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
47 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
48 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
49 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
50 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
51 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
52 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
53 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
54 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
55 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
56 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
57 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
58 | int, int, int, int, int, int> |
59 | v_300_types_no_value; |
60 | |
61 | v1 = 12; // v contains int |
62 | v_v1 = v1 ; |
63 | int i = std::get<int>(v1); |
64 | printf( "%d\n" , i ); // break here |
65 | |
66 | v2 = 2.0 ; |
67 | double d = std::get<double>(v2) ; |
68 | printf( "%f\n" , d ); |
69 | |
70 | v3 = 'A' ; |
71 | char c = std::get<char>(v3) ; |
72 | printf( "%d\n" , c ); |
73 | |
74 | // Checking v1 above and here to make sure we done maintain the incorrect |
75 | // state when we change its value. |
76 | v1 = 2.0; |
77 | d = std::get<double>(v1) ; |
78 | printf( "%f\n" , d ); // break here |
79 | |
80 | try { |
81 | v_no_value.emplace<0>(S()); |
82 | } catch( ... ) {} |
83 | |
84 | printf( "%zu\n" , v_no_value.index() ) ; |
85 | |
86 | try { |
87 | v_300_types_no_value.emplace<0>(S()); |
88 | } catch (...) { |
89 | } |
90 | |
91 | printf("%zu\n" , v_300_types_no_value.index()); |
92 | #endif |
93 | |
94 | return 0; // break here |
95 | } |
96 | |