C++ features by examples
20.cpp
Go to the documentation of this file.
1
15static_assert(__cplusplus >= 201707);
16
17#include <bits/stdc++.h>
18
19using namespace std;
20
27#if __cpp_lib_three_way_comparison
28static_assert(1 <=> 2 < 0 );
29static_assert(2 <=> 1 > 0 );
30static_assert(1 <=> 1 == 0 );
31#else
32#pragma message("undefined __cpp_lib_three_way_comparison")
33#endif
34
35#if __cpp_char8_t
37char8_t char8;
38#endif
39
41
42auto to_array_demo = to_array("foo");
43static_assert(to_array_demo.size() == 4);
44
45void init_20()
46{
47 struct point { int x, y; };
48 struct line { point a, b; };
49
50 // [aggregate_initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization)
51
52#if __cplusplus > 201707
53 point p1 = { .x = 1 };
54 assert(p1.x == 1);
55 assert(!p1.y);
56
57 point p2 { {} , 2 };
58 assert(p2.y == 2);
59 assert(!p2.x);
60#endif
61#if __cpp_aggregate_paren_init >= 201902
62 int a[] (0, 1, 2);
63 assert(a[2] == 2);
64 line l2 = { 1, 2 };
65 assert(l2.a.x == 1);
66 assert(l2.a.y == 2);
67#endif
68 line l1 = { };
69 assert(!l1.a.x);
70 line l3 = { 1, 2, 3, 4 };
71 assert(l3.b.x == 3);
72 assert(l3.b.y == 4);
73}
74
76{
77#if __cpp_lib_bit_cast
78 cout << typeid(bit_cast<double>(0));
79#endif
80}
81
83{
86 auto a = make_shared<int[10]>();
87 static_assert(is_same_v<decltype(a.get()), int*>);
88
90 map<int, int> m = {{2,3}};
91#if __cplusplus > 201707 && __GNUG__ > 8
92 assert(m.contains(2));
93#endif
94#if __cpp_lib_erase_if
96 vector<int> v = {11, 12, 13};
97 assert(erase_if(v, [](int a) {return a >= 13;}) == 1);
98 assert(v.size() == 2);
99#endif
100}
101
102namespace lambda {
103
104#if __cpp_template_template_args && __GNUG__ > 8
106template <typename ... Args>
107auto make_lambda_with_parameter_pack_capture(Args&& ... args)
108{
109 return [... args = forward<Args>(args)] {
110 return (... + args);
111 };
112}
113#endif
114
116{
117#if __cplusplus >= 201709
118 // generic lambda, operator() is a template with two parameters
119 auto glambda = []<class T>(T a, auto&& b) { return a < b; };
120 assert(glambda(1,2));
121 // generic lambda, operator() is a template with one parameter pack
122 auto f = []<typename ...Ts>(Ts&& ...ts) {
123 return 1;
124 };
125 assert(f(1,2,3));
126
127 struct point { int x, y; };
128 auto point_lambda = []<class T=point>(T&& var) {};
129 point_lambda({1, 2});
130
131 #if __cpp_template_template_args && __GNUG__ > 8
132 assert(make_lambda_with_parameter_pack_capture(1,2,3)() == 6);
133 #endif
134#endif
135}
136
137}
138using namespace lambda;
139
140
142
167// Using [requires](https://en.cppreference.com/w/cpp/keyword/requires)
168
169template <typename T> requires is_integral_v<T> T constexpr requires_demo(T a) { return a + 1; }
170
171static_assert(requires_demo(1) == 2);
172
177template <typename T> auto constexpr requires_demo(T && a) requires is_same_v<T, double> { return 2; }
178static_assert(requires_demo(0.1) == 2);
179
180// Annotated example of complex requirement
181template <class T>
182requires // requires-clause
183is_signed_v<T> || (is_unsigned_v<T> && ! is_void_v<void>) // constraint expression
185
187
188template < class T >
189requires // requires-clause
190requires() // requires-expression
191{ true;} // unevaluated requirements sequence