-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp17Test.cpp
More file actions
374 lines (307 loc) · 8.37 KB
/
Copy pathcpp17Test.cpp
File metadata and controls
374 lines (307 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// Created by william on 2021/4/1.
//
#include "base.h"
#include <iostream>
#include <memory>
using namespace std;
void reference17()
{
static_assert(is_reference_v<int&>);
// L-value:
static_assert(is_lvalue_reference_v<int&>);
// R-value
static_assert(is_rvalue_reference_v<int&&>);
}
#if __cpp_deduction_guides
/// @brief pair<int, double> p(1, 2.3);
constexpr pair deducted_pair(1, 2.3);
static_assert(deducted_pair.second == 2.3);
/// @brief auto t = make_tuple(4, 3, 2.5);
constexpr tuple deducted_tuple(4, 2, 2.5);
static_assert(get<2>(deducted_tuple) == 2.5);
/// @brief template with type float by default
template <typename T = float>
struct template_struct
{
T val;
template_struct() :
val() {}
template_struct(T val) :
val(val) {}
};
/// @brief deducted \<int\>
template_struct template_arg_deduction{ 1 };
#if __cpp_deduction_guides
/// @brief deducted \<float\>
template_struct template_default_arg_deduction;
#endif
vector<int> int_vector = { 1, 2, 3, 4 };
/// @brief deduced deque\<int\>
deque deduction_guide1_queue(int_vector.begin(), int_vector.end());
/// @brief deduced deque\<vector\<int> :: iterator\>
deque deduction_guide2_queue{ int_vector.cbegin(), int_vector.cend() };
/// [deduction_guides](https://en.cppreference.com/w/cpp/container/array/deduction_guides)
array deduction_guide_array{ 1, 2, 3, 4 };
/// @brief deduced vector\<int\>
vector deduction_guide1_vector(int_vector.begin(), int_vector.end());
/// @brief deduced vector\<vector\<int\> :: iterator\>
vector deduction_guide2_vector{ int_vector.begin(), int_vector.end() };
void deduction_guides_17()
{
assert(deduction_guide1_queue[0] == 1);
assert(*deduction_guide2_queue[0] == 1);
assert(deduction_guide1_vector[0] == 1);
assert(*deduction_guide2_vector[0] == 1);
}
#else
#pragma message("undefined __cpp_deduction_guides")
#endif
template <auto n>
struct B
{ /* ... */
};
B<5> b1; // OK: non-type template parameter type is int
B<'a'> b2;
// Error:
// B<2.5> b3;
tuple<int, int> foo_tuple()
{
// return make_tuple(1, -1);
return { 1, -1 };
}
template <auto... seq>
struct my_integer_sequence
{
};
/// @brief auto seq = integer_sequence<int, 0, 1, 2>();
auto seq = my_integer_sequence<0, 1, 2>();
// explicit constexpr
auto identity = [](int n) constexpr
{
return n;
};
//static_assert(identity(1) == 1);
// lambda with auto argument is actually a template
// implicit auto constexpr:
auto can_be_constexpr1 = [](auto a) { return a; };
auto can_be_constexpr2 = [](int (*fp)(int), auto a) { return fp(a); };
static_assert(can_be_constexpr2(can_be_constexpr1, 3) == 3);
// error: non-constant condition for static assertion
// static int i=0;
// static_assert(can_be_constexpr2(can_be_constexpr1, i) == 0);
auto non_const = [](auto a) {static int s; return a; };
constexpr int const_inc(int n)
{
return [n] { return n + 1; }();
}
constexpr int (*inc)(int) = const_inc;
static_assert(const_inc(1) == 2);
void capture_this_by_value()
{
struct capture_value_o
{
int value{ 1 };
auto get_value_copy()
{
return [*this] { return value; };
}
};
capture_value_o mo;
auto val = mo.get_value_copy();
mo.value = 2;
assert(val() == 1);
}
void lambda_17()
{
assert(can_be_constexpr1(2) == 2);
int i = 3;
assert(can_be_constexpr2(can_be_constexpr1, 3) == 3);
non_const(1);
capture_this_by_value();
}
template <typename... Args>
constexpr bool folding_and(Args... args)
{
return (true && ... && args);
}
template <typename... Args>
constexpr auto folding_sum(Args... args)
{
return (... + args);
}
void folding_demo()
{
static_assert(!folding_and(true, false, true));
static_assert(folding_sum(1.0, 2.0f, 3) == 6.0);
}
/// Before: namespace other { namespace other2 {
namespace outer_namespace::qualified_nested_namespace
{
int in_qualified_nested_namespace;
}
static_assert(__cpp_structured_bindings);
void structured_bindings()
{
int a[2] = { 1, 2 };
auto [a0, a1] = a;
// a0_ref = &a; a0_ref = &a;
auto& [a0_ref, a1_ref] = a;
float x{};
char y{};
int z{};
unordered_map<string, int> mapping{
{ "a", 1 },
{ "b", 2 },
{ "c", 3 },
};
for (const auto& [key, value] : mapping)
{
// Do something with key and value
}
// tuple<float&,char&&,int> tpl(x,move(y),z);
// const auto& [a,b,c] = tpl;
}
void conditional_with_init()
{
if (auto a = true)
{
};
switch (int a = 10)
{
}
}
// Will warn if return of foo() is ignored
[[nodiscard]] int foo()
{
return 0;
}
void test_attr()
{
int a{ 1 };
switch (a)
{
// Indicates that falling through on case 1 is intentional
case 1:
[[fallthrough]];
case 2:
// Indicates that b might be unused, such as on production builds
[[maybe_unused]] int b = foo();
assert(b > 0);
break;
}
}
void constif()
{
if constexpr (true)
printf("1\n");
}
static char char_u8 = u8'x';
enum byte_e : unsigned char
{
};
static byte_e b{ 123 };
void types_17()
{
static_assert(is_integral_v<int>);
static_assert(__cpp_hex_float);
double hex_double = 0x1.2p3; // https://en.cppreference.com/w/cpp/language/floating_literal
assert(hex_double == 9.0);
static_assert(is_invocable<decltype(types_17)>::value);
static_assert(is_invocable<int()>::value);
static_assert(is_invocable_r<int, int()>::value);
static_assert(is_invocable_r<void, void(int), int>::value);
static_assert(negation_v<bool_constant<false>>);
auto inc = [](int a) -> int { return a + 1; };
static_assert(is_invocable_r<int, decltype(inc), int>::value);
static_assert(__cpp_lib_invoke);
// https://en.cppreference.com/w/cpp/utility/functional/invoke
assert(invoke(inc, 2) == 3);
}
void map_demo()
{
/// @brief https://en.cppreference.com/w/cpp/container/map/extract
map<int, string> m{ { 1, "mango" }, { 2, "papaya" }, { 3, "guava" } };
auto nh = m.extract(2);
nh.key() = 4;
m.insert(move(nh));
// m == {{1, "mango"}, {3, "guava"}, {4, "papaya"}}
set<int> src{ 1, 3, 5 };
set<int> dst{ 2, 4, 5 };
dst.merge(src);
}
void variant_demo()
{
variant<int, float> v, w;
v = 12; // v contains int
int i = get<int>(v);
w = get<int>(v);
w = get<0>(v); // same effect as the previous line
w = v; // same effect as the previous line
try
{
get<float>(w); // w contains int, not float: will throw
}
catch (const bad_variant_access&)
{}
using namespace literals;
variant<string> x("abc");
// converting constructors work when unambiguous
x = "def"; // converting assignment also works when unambiguous
variant<string, void const*> y("abc");
// casts to void const * when passed a char const *
assert(holds_alternative<void const*>(y));
y = "xyz"s;
assert(holds_alternative<string>(y));
}
void clamp_demo()
{
static_assert(__cpp_lib_clamp);
/// clamp(x, low, high) == x < low ? low : x > high ? high : x;
assert(clamp(0, 1, 3) == 1);
assert(clamp(2, 1, 3) == 2);
assert(clamp(4, 1, 3) == 3);
}
void dynamic_memory_17()
{
int d[2] = { 10, 11 };
unique_ptr<int[]> u1(d);
assert(u1.get()[0] == 10); // C++11
assert(u1[1] == 11);
assert((bool)u1);
u1.release();
const shared_ptr<int> s1{ 0 };
// assert(typeid(reinterpret_pointer_cast<shared_ptr<int>>(s1)).name() == string("St10shared_ptrIS_IiEE"));
vector<int> s = { 5, 7, 4, 2, 8, 6, 1, 9, 0, 3 };
int a[] = { 1, 2, 3 };
// valarray va(a, 3); // uses explicit deduction guide
// static_assert(is_integral_v<remove_reference<decltype(va[0])>::type>);
}
void string_view_demo()
{
static_assert(__cpp_lib_string_view);
string s = "abcd";
string_view v = s;
assert(v.data() == s.c_str());
assert(v.substr(1, 2).data() >= s.c_str());
assert(v.substr(1, 2).data() <= s.c_str() + s.length());
}
inline int inline_var;
void cpp17Test()
{
reference17();
deduction_guides_17();
folding_demo();
lambda_17();
variant_demo();
clamp_demo();
dynamic_memory_17();
string_view_demo();
// for (auto&& [first,second] : mymap) { }
types_17();
/// https://en.cppreference.com/w/cpp/language/range-for
map<int, int> mymap;
for (auto&& [first, second] : mymap) {
// use first and second
}
}