1 | |
---|
2 | #include <optional> |
---|
3 | #include <any> |
---|
4 | #include <iostream> |
---|
5 | #include <string_view> |
---|
6 | #include <variant> |
---|
7 | #include <type_traits> |
---|
8 | #include <numeric> |
---|
9 | #include <limits> |
---|
10 | #include <string> |
---|
11 | #include <cassert> |
---|
12 | #include <cstdint> |
---|
13 | |
---|
14 | namespace |
---|
15 | { |
---|
16 | // func is enabled if all Ts... have the same type as T |
---|
17 | template < typename T, typename... Ts > |
---|
18 | std::enable_if_t< std::conjunction_v< std::is_same< T, Ts >... > > func( T, Ts... ) |
---|
19 | { |
---|
20 | std::cout << "all types in pack are T\n"; |
---|
21 | } |
---|
22 | // otherwise |
---|
23 | template < typename T, typename... Ts > |
---|
24 | std::enable_if_t< !std::conjunction_v< std::is_same< T, Ts >... > > func( T, Ts... ) |
---|
25 | { |
---|
26 | std::cout << "not all types in pack are T\n"; |
---|
27 | } |
---|
28 | struct S |
---|
29 | { |
---|
30 | mutable int x1 : 2; |
---|
31 | double y1; |
---|
32 | }; |
---|
33 | S f() { return S { 1, 2.3 }; } |
---|
34 | } // namespace |
---|
35 | |
---|
36 | void |
---|
37 | test_structured_bindings() |
---|
38 | { |
---|
39 | const auto [ x, y ] = f(); // x is an int lvalue identifying the 2-bit bit field |
---|
40 | // y is a const volatile double lvalue |
---|
41 | std::cout << x << ' ' << y << '\n'; // 1 2.3 |
---|
42 | x = -2; // OK |
---|
43 | // y = -2.; // Error: y is const-qualified |
---|
44 | std::cout << x << ' ' << y << '\n'; // -2 2.3 |
---|
45 | } |
---|
46 | |
---|
47 | void |
---|
48 | test_conjuction() |
---|
49 | { |
---|
50 | func( 1, 2, 3 ); |
---|
51 | func( 1, 2.0, "test_conjuction" ); |
---|
52 | } |
---|
53 | |
---|
54 | void |
---|
55 | test_constexpr_lambda() |
---|
56 | { |
---|
57 | auto Fwd = []( int ( *fp )( int ), auto a ) { return fp( a ); }; |
---|
58 | auto C = []( auto a ) { return a; }; |
---|
59 | static_assert( Fwd( C, 3 ) == 3 ); // OK |
---|
60 | } |
---|
61 | |
---|
62 | void |
---|
63 | test_variant() |
---|
64 | { |
---|
65 | std::variant< int, float > v, w; |
---|
66 | v = 42; // v contains int |
---|
67 | int i = std::get< int >( v ); |
---|
68 | std::cout << "variant<int> = " << i << std::endl; |
---|
69 | assert( 42 == i ); // succeeds |
---|
70 | w = std::get< int >( v ); |
---|
71 | w = std::get< 0 >( v ); // same effect as the previous line |
---|
72 | w = v; // same effect as the previous line |
---|
73 | |
---|
74 | try |
---|
75 | { |
---|
76 | std::get< float >( w ); // w contains int, not float: will throw |
---|
77 | } |
---|
78 | catch ( const std::bad_variant_access &ex ) |
---|
79 | { |
---|
80 | std::cout << "test_variant: " << ex.what() << '\n'; |
---|
81 | } |
---|
82 | |
---|
83 | using namespace std::literals; |
---|
84 | |
---|
85 | // converting constructors work when unambiguous |
---|
86 | std::variant< std::string > x( "abc" ); |
---|
87 | // converting assignment also works when unambiguous |
---|
88 | x = "def"; |
---|
89 | |
---|
90 | std::variant< std::string, void const * > y( "abc" ); |
---|
91 | // casts to void const * when passed a char const * |
---|
92 | assert( std::holds_alternative< void const * >( y ) ); // succeeds |
---|
93 | y = "xyz"s; |
---|
94 | assert( std::holds_alternative< std::string >( y ) ); // succeeds |
---|
95 | } |
---|
96 | |
---|
97 | void |
---|
98 | test_optional() |
---|
99 | { |
---|
100 | std::optional< int > i = 321; |
---|
101 | std::cout << "optional i = " << i.value() << std::endl; |
---|
102 | } |
---|
103 | |
---|
104 | void |
---|
105 | test_any() |
---|
106 | { |
---|
107 | // any type |
---|
108 | std::any a = 1; |
---|
109 | std::cout << a.type().name() << ": " << std::any_cast< int >( a ) << '\n'; |
---|
110 | a = 3.14; |
---|
111 | std::cout << a.type().name() << ": " << std::any_cast< double >( a ) << '\n'; |
---|
112 | a = true; |
---|
113 | std::cout << a.type().name() << ": " << std::any_cast< bool >( a ) << '\n'; |
---|
114 | a = "FooBar"; |
---|
115 | std::cout << a.type().name() << ": " << std::any_cast< const char* >( a ) << '\n'; |
---|
116 | } |
---|
117 | |
---|
118 | void |
---|
119 | test_string_view() |
---|
120 | { |
---|
121 | |
---|
122 | constexpr std::string_view unicode[] { "▀▄─", "▄▀─", "▀─▄", "▄─▀" }; |
---|
123 | |
---|
124 | for ( int y {}, p {}; y != 6; ++y, p = ( ( p + 1 ) % 4 ) ) |
---|
125 | { |
---|
126 | for ( int x {}; x != 16; ++x ) { std::cout << unicode[ p ]; } |
---|
127 | std::cout << '\n'; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | void |
---|
132 | test_limits() |
---|
133 | { |
---|
134 | const auto l_int = std::numeric_limits< std::int32_t >::lowest(); |
---|
135 | const auto h_int = std::numeric_limits< std::int32_t >::max(); |
---|
136 | |
---|
137 | const auto l_short = (std::int16_t)l_int; |
---|
138 | const auto h_short = (std::int16_t)h_int; |
---|
139 | |
---|
140 | std::cout << l_int << " " << l_short << std::endl; |
---|
141 | std::cout << h_int << " " << h_short << std::endl; |
---|
142 | } |
---|
143 | |
---|
144 | int |
---|
145 | main() |
---|
146 | { |
---|
147 | test_variant(); |
---|
148 | test_optional(); |
---|
149 | test_any(); |
---|
150 | test_string_view(); |
---|
151 | test_constexpr_lambda(); |
---|
152 | test_conjuction(); |
---|
153 | test_structured_bindings(); |
---|
154 | test_limits(); |
---|
155 | return 0; |
---|
156 | } |
---|