1 | #include <cstdio> |
2 | #include <string> |
3 | #include <variant> |
4 | #include <vector> |
5 | |
6 | struct S { |
7 | operator int() { throw 42; } |
8 | }; |
9 | |
10 | int main() { |
11 | bool has_variant = true; |
12 | |
13 | printf(format: "%d\n" , has_variant); // break here |
14 | |
15 | std::variant<int, double, char> v1; |
16 | std::variant<int, double, char> &v1_ref = v1; |
17 | std::variant<int, double, char> v2; |
18 | std::variant<int, double, char> v3; |
19 | std::variant<std::variant<int, double, char>> v_v1; |
20 | std::variant<int, double, char> v_no_value; |
21 | // The next variant has many types, meaning the type index does not fit in |
22 | // a byte and must be `unsigned short` instead of `unsigned char` when |
23 | // using the unstable libc++ ABI. With stable libc++ ABI, the type index |
24 | // is always just `unsigned int`. |
25 | std::variant< |
26 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
27 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
28 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
29 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
30 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
31 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
32 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
33 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
34 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
35 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
36 | int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, |
37 | int, 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, int, |
39 | int, 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, int, |
41 | int, 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> |
43 | v_many_types_no_value; |
44 | |
45 | v1 = 12; // v contains int |
46 | v_v1 = v1; |
47 | int i = std::get<int>(v&: v1); |
48 | printf(format: "%d\n" , i); // break here |
49 | |
50 | v2 = 2.0; |
51 | double d = std::get<double>(v&: v2); |
52 | printf(format: "%f\n" , d); |
53 | |
54 | v3 = 'A'; |
55 | char c = std::get<char>(v&: v3); |
56 | printf(format: "%d\n" , c); |
57 | |
58 | // Checking v1 above and here to make sure we done maintain the incorrect |
59 | // state when we change its value. |
60 | v1 = 2.0; |
61 | d = std::get<double>(v&: v1); |
62 | printf(format: "%f\n" , d); // break here |
63 | |
64 | try { |
65 | v_no_value.emplace<0>(args: S()); |
66 | } catch (...) { |
67 | } |
68 | |
69 | printf(format: "%zu\n" , v_no_value.index()); |
70 | |
71 | try { |
72 | v_many_types_no_value.emplace<0>(args: S()); |
73 | } catch (...) { |
74 | } |
75 | |
76 | printf(format: "%zu\n" , v_many_types_no_value.index()); |
77 | |
78 | return 0; // break here |
79 | } |
80 | |