forked from makelinux/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.cpp
More file actions
478 lines (361 loc) · 9.36 KB
/
11.cpp
File metadata and controls
478 lines (361 loc) · 9.36 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
@file
@brief C++11
@defgroup CPP11 C++11 examples
https://en.wikipedia.org/wiki/C++11
https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md
@{
*/
static_assert(__cplusplus == 201103, "");
#include <utility>
#include <iostream>
#include <cassert>
#include <functional>
#include <algorithm>
#include <array>
#include <memory>
#include <thread>
#if __has_include (<version>)
#include <version>
#endif
using namespace std;
/**
@defgroup lang11 Language
@{
*/
void types_11()
{
static_assert(__cpp_decltype, "");
int a;
// https://en.cppreference.com/w/cpp/header/type_traits
decltype(a) b; // https://en.cppreference.com/w/cpp/language/decltype
assert((is_same<decltype(a), decltype(b)>::value));
assert((!is_same<decltype(a), unsigned>::value));
assert((is_same<int, int32_t>::value));
assert((is_same<signed, int32_t>::value));
assert(is_integral<int>::value);
assert(is_integral<bool>::value);
assert(!is_integral<float>::value);
assert(is_pointer<int*>::value);
assert(sizeof (long long) >= 8);
}
void dynamic_memory_11()
{
int d = 0;
unique_ptr<int> u1; // https://en.cppreference.com/w/cpp/memory/unique_ptr
assert(!u1);
u1.reset(&d);
assert(u1);
*u1 = 1;
assert(d == 1);
unique_ptr<int> u2;
// u2 = u1; - prohibited
int * p1 = u1.get();
u2 = move(u1); // https://en.cppreference.com/w/cpp/utility/move
assert(u2.get() == p1);
assert(u2);
assert(!u1);
assert(u2.get() == &d);
// must release because d is local
u2.release();
u2.reset(new int(10));
assert(*u2 == 10);
u2.reset(); // deletes int(10)
assert(u2 == nullptr); // https://en.cppreference.com/w/cpp/language/nullptr
assert(!u2);
shared_ptr<int> s1;
assert(!s1);
assert(!s1.use_count());
auto s2 = make_shared<int>(1);
assert(s2.use_count() == 1);
s1 = s2;
assert(s1.use_count() == 2);
*s1 = 2;
assert(*s1 == *s1.get());
assert(*s2 == 2);
s2.reset();
assert(s1.use_count() == 1);
assert(!s2.use_count());
}
/// func_type - overloaded functions
char func_type(const int& x)
{
assert(is_const<typename remove_reference<decltype(x)>::type>::value);
assert(is_lvalue_reference<decltype(x)>::value);
return 'C';
}
char func_type(int& x)
{
assert(is_lvalue_reference<decltype(x)>::value);
return 'L';
}
char func_type(int&& x)
{
assert(is_rvalue_reference<decltype(x)>::value);
return 'R';
}
template<class T>
char func_type_template(T&& x) // x is a forwarding reference
{
// x is not R-value here
assert(func_type(x) != 'R');
// x can be forwarded as R or L value
// https://en.cppreference.com/w/cpp/utility/forward
return func_type(forward<T>(x)); // like func_type((T)(x));
}
void references_11()
{
// https://en.cppreference.com/w/cpp/language/reference
assert(is_reference<int&>::value);
// L-value:
assert(is_lvalue_reference<int&>::value);
// R-value
assert(is_rvalue_reference<int&&>::value);
const int c = 1;
int i;
assert(func_type(c) == 'C');
assert(func_type(i) == 'L');
assert(func_type(1) == 'R');
assert(func_type_template(c) == 'C');
assert(func_type_template(i) == 'L');
assert(func_type_template(1) == 'R');
}
void init_11()
{
// https://en.cppreference.com/w/cpp/language/direct_initialization
// https://en.cppreference.com/w/cpp/language/list_initialization
class C { public: int a, b, c; };
auto o2 = C{1, 2, 3};
C o3{1, 2, 3};
(void) o3;
// https://en.cppreference.com/w/cpp/language/zero_initialization
auto z1 = C();
C z2 = {};
auto z3 = C{};
assert(!z1.a);
assert(!z2.a);
assert(!z3.a);
}
/**
Trailing return type
https://en.cppreference.com/w/cpp/language/function
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/rzarg/trailing_return.htm
*/
/// https://en.cppreference.com/w/cpp/language/auto
auto auto_int = 1;
// int before(int a) { return a; }
auto trailing_return_type(int a) -> int
{
return a;
}
void func_11()
{
class functor {
int y = 1;
public:
int operator()(int a) const {
return a + y;
}
};
functor ft;
assert(ft(1) == 2);
// https://en.cppreference.com/w/cpp/utility/functional/function
function<int(int)> ft2 = ft;
assert(ft(2) == 3);
return;
// https://en.cppreference.com/w/cpp/utility/functional/bind
auto binded = bind(ft2, 3);
assert(binded() == 5);
}
static_assert(__cpp_constexpr, "");
/// https://en.cppreference.com/w/cpp/language/constexpr
constexpr int constexpr_factorial(int n)
{
return n <= 1 ? 1 : (n * constexpr_factorial(n - 1));
}
/// https://en.cppreference.com/w/cpp/language/parameter_pack
template<typename T>
T constexpr adder(T v) {
return v;
}
template<typename T, typename... Args>
T constexpr adder(T first, Args... args) {
return first + adder(args...);
}
static_assert(adder(1,2,3) == 6,"");
struct Base11
{
virtual void method1();
virtual void method2();
};
struct Derived11 : Base11
{
void method1() override; ///< [ref](https://en.cppreference.com/w/cpp/language/override)
void method2() final; ///< [ref](https://en.cppreference.com/w/cpp/language/final)
};
/// @}
/**
@defgroup lambda11 Lambda
https://en.cppreference.com/w/cpp/language/lambda
https://www.geeksforgeeks.org/lambda-expression-in-c/
@{
*/
static void lambda_basics(void)
{
auto annotated_named_lambda_expression = // optional name
[ ] // capture
( ) // optional list of arguments
{ }; // body
// Primitive named lambdas are just like closure functions:
// https://en.wikipedia.org/wiki/Closure_(computer_programming)
// declaration like a function:
// void closure() { };
auto closure = [] { };
closure();
// with arguments
auto pass = [] (int a) { return a; };
assert(pass(5) == 5);
// lambda captures external value
int c = 1;
auto get_i = [=] () { return c; };
assert(get_i() == 1);
// lambda captures external variable by reference
// with omitted arguments and return type
auto inc_get = [&] { return ++c; };
assert(inc_get() == 2);
assert(inc_get() == 3);
// annotated expanded empty inline lambda call:
[ ] // capture
( ) // optional list of arguments
-> void // optional return value
{ } // body
( ); // call with arguments
// annotated expanded sample inline lambda call:
c = // result
[c] // capture
(int a) // optional list of arguments
-> int // optional return value
{ return c + a; } // body
(1); // call with argument
assert(c == 4);
// inline lambda which is called in place
// https://en.wikipedia.org/wiki/Anonymous_function
// assert((1 + 1) == 2);
assert([](int a) { return a + 1; }(1) == 2);
// Actually calling lambda inline is useless
// and is provided only for demonstration.
}
/// @cond
static int glob;
/// @endcond
static void lambda_capture(void)
{
// read only
int i = 2;
assert([=]{return i; }() == 2);
// read and write access
[&](int a){i = a;}(3);
assert(i == 3);
// explicit r/o and r/w
int j;
[i, &j](){ j = i; }();
assert(j == i);
// r/o by default
i++;
[=, &j](){ j = i; }();
assert(j == i);
// r/w by default
i++;
[&, i](){ j = i; }();
assert(j == i);
// can access globals anyway
auto inc_global = [] () { return ++glob; };
assert(inc_global() == 1);
assert(inc_global() == 2);
}
/// Compare with @ref sort_03
void sort_11()
{
array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
sort(s.begin(), s.end(),
// sort using a lambda expression
[](int a, int b)
{ return a > b; }
);
}
/// @}
/**
@defgroup lambda11_complex More complex Lambdas
@{
*/
/// @cond
/// https://en.cppreference.com/w/cpp/utility/functional/function
// int use_lambda(int a; int (func*)(int))
static int use_lambda(int a, function<int(int)> f)
{
// lambda argument is like pointer to functions
return f(a);
}
static function<int(int)> g_f;
static void set_lambda(function<int(int)> &&f)
{
g_f = f;
}
static int call_lambda(int a)
{
return g_f(a);
}
/// @endcond
static void lambda_complex(void)
{
auto increment = [] (int a) -> int { return a + 1; };
assert(increment(5) == 6);
// named lambda as argument
assert(use_lambda(2, increment) == 3);
set_lambda(increment);
assert(call_lambda(3) == 4);
// inline lambda as argument
assert(use_lambda(1, [](int a) {return a + 1;}) == 2);
}
/// @} lambda11_complex
/**
@{
TODO:
function<int(void)> get_a = a;
https://en.cppreference.com/w/cpp/language/move_constructor
https://en.cppreference.com/w/cpp/language/move_assignment
https://en.cppreference.com/w/cpp/language/data_members#Member_initialization
https://en.cppreference.com/w/cpp/language/attributes
https://en.cppreference.com/w/cpp/language/string_literal
https://en.cppreference.com/w/cpp/language/character_literal
https://en.cppreference.com/w/cpp/language/user_literal
https://en.cppreference.com/w/cpp/language/aggregate_initialization
https://en.cppreference.com/w/cpp/language/initializer_list
https://en.cppreference.com/w/cpp/language/range-for
https://en.cppreference.com/w/cpp/utility/declval
https://en.cppreference.com/w/cpp/language/range-for
https://en.cppreference.com/w/cpp/types/is_move_constructible
https://en.cppreference.com/w/cpp/types/is_constructible
https://en.cppreference.com/w/cpp/utility/functional/ref
https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
@}
*/
int main(void)
{
references_11();
init_11();
auto r = trailing_return_type(1);
(void) r;
lambda_basics();
lambda_capture();
lambda_complex();
func_11();
sort_11();
dynamic_memory_11();
static_assert(constexpr_factorial(4), "");
types_11();
thread t([]{ });
t.join();
return 0;
}
/// @}