-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
395 lines (342 loc) · 10.6 KB
/
main.cpp
File metadata and controls
395 lines (342 loc) · 10.6 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
/**
* All examples are taken from C++ Senioreas blog
* https://cppsenioreas.wordpress.com/
*
* Examples from:
* cpp_senioreas::lambda_expressions_and_between_them_from_11_to_20_part_1
* https://cppsenioreas.wordpress.com/2020/09/07/lambda-expressions-and-between-them-from-cpp11-to-cpp20-part-1/
*
* cpp_senioreas::lambda_expressions_and_between_them_the_lambda_mystery_part_2
* https://cppsenioreas.wordpress.com/2020/09/13/lambda-expressions-and-between-them-the-lambdas-mystery-part-2/
*/
#include <iostream>
#include <thread>
#include <algorithm>
#include <vector>
#include <type_traits>
// Example #
#define EXAMPLE_3
#if __cplusplus <= 201103L // C++11
class my_class {
public:
void create_thread() {
std::thread t(my_thread_function, std::ref(*this));
t.join();
}
void create_thread_2() {
std::thread t([this]() {
std::cout << this->my_int << std::endl;
});
t.join();
}
void create_thread_3() {
std::thread t([this]() {
this->non_static_function();
});
t.join();
}
private:
int my_int;
static void my_thread_function(my_class& mc) { // This function has to be a static function in order to used as a thread function.
std::cout << mc.my_int << std::endl; // In order to access a class member variable, we have to accept a class reference as a function argument.
}
void non_static_function() const {
std::cout << my_int << std::endl;
}
};
#elif __cplusplus > 201103L && __cplusplus <= 201402L // C++14
#ifdef EXAMPLE_4
class __lambda_4_16 {
public:
template<class type_parameter_0_0, class type_parameter_0_1>
inline auto operator()(type_parameter_0_0 a, type_parameter_0_1 b) const {
return a + b;
}
// template<> // C++ doesn't allow explicit specialization in non-namespace scope. This workaround suggested by @bop on StackOverflow: https://stackoverflow.com/a/13018943/8038186
inline double operator()(int a, double b) const {
return static_cast<double>(a) + b;
}
// template<> // C++ doesn't allow explicit specialization in non-namespace scope. This workaround suggested by @bop on StackOverflow: https://stackoverflow.com/a/13018943/8038186
inline float operator()(float a, int b) const {
return a + static_cast<float>(b);
}
private:
template<class type_parameter_0_0, class type_parameter_0_1>
static inline auto __invoke(type_parameter_0_0 a, type_parameter_0_1 b) {
return a + b;
}
public:
inline constexpr __lambda_4_16(__lambda_4_16 &&) noexcept = default;
};
#endif
#ifdef EXAMPLE_5
#endif
#elif __cplusplus > 201402L && __cplusplus <= 201703L // C++17
#else // C++20
template <typename T>
concept Numeric = std::is_arithmetic_v<T>;
template <typename T>
concept PlusOperator = requires(T type) {
{ type + type };
};
#endif
void thread_function(int a, int b) { std::cout << a + b << std::endl; }
int main() {
#if __cplusplus <= 201103L // C++11
#ifdef EXAMPLE_0
[ /* capture */ ] ( /* params */ ) /* specifiers */ /* exceptions */ -> void /* return */ {
/* body */
};
auto sum = [](int a, int b) [[]] -> int {
return a + b;
};
#endif
#ifdef EXAMPLE_1
std::thread t(thread_function, 3, 5);
t.join();
#endif
#ifdef EXAMPLE_2
std::vector<int> vec = {1, 2, 3};
std::for_each(vec.begin(), vec.end(), [](int &elem) {
elem *= elem;
});
std::for_each(vec.begin(), vec.end(), [](int &elem) {
std::cout << elem << " ";
});
std::cout << std::endl;
#endif
#ifdef EXAMPLE_3
int a, b;
a = 1; b = 2;
auto lambda_1 = [=] { std::cout << a << " " << b << std::endl; };
auto lambda_2 = [&] { std::cout << a << " " << b << std::endl; };
lambda_1();
lambda_2();
a = 4; b = 5;
lambda_1();
lambda_2();
#endif
#ifdef EXAMPLE_4
int a = 4;
auto increase_local_a = [a]() mutable {
std::cout << a << std::endl;
a++;
};
auto decrease_ref_a = [&a]() mutable {
std::cout << a << std::endl;
a--;
};
increase_local_a(); // Prints: 4
decrease_ref_a(); // Prints: 4
increase_local_a(); // Prints: 5
decrease_ref_a(); // Prints: 3
increase_local_a(); // Prints: 6
decrease_ref_a(); // Prints: 2
std::cout << a << std::endl; // Prints: 1
#endif
#ifdef EXAMPLE_5
int a = 5;
class __lambda_5_17 {
public:
inline int operator()(int n1, int n2) const {
return a * (n1 + n2);
}
private:
int &a;
public:
inline __lambda_5_17(const __lambda_5_17 &ref) noexcept : a(ref.a) {
std::cout << "Copy CTOR" << std::endl;
}
inline __lambda_5_17(__lambda_5_17 && ref) noexcept : a(ref.a) {
std::cout << "Move CTOR" << std::endl;
};
__lambda_5_17(int &_a) : a{_a} {
std::cout << "Parametrize CTOR" << std::endl;
}
};
__lambda_5_17 lambda = __lambda_5_17(__lambda_5_17{a});
auto aaa = std::move(lambda);
auto bbb = aaa;
std::cout << aaa.operator()(5, 6) << std::endl;
a++;
std::cout << aaa.operator()(5, 6) << std::endl;
std::cout << bbb.operator()(5, 6) << std::endl;
#endif
#ifdef EXAMPLE_6
class __lambda_4_17 {
public:
inline int operator()(int n1, int n2) const {
return n1 + n2;
}
using retType_4_17 = int (*)(int, int);
inline operator retType_4_17() const noexcept {
return __invoke;
}
private:
static inline int __invoke(int n1, int n2) {
return n1 + n2;
}
public:
inline constexpr __lambda_4_17(const __lambda_4_17 &) noexcept = default;
inline constexpr __lambda_4_17(__lambda_4_17 &&) noexcept = default;
};
__lambda_4_17 lambda = __lambda_4_17(__lambda_4_17{});
__lambda_4_17 moved_lambda = __lambda_4_17(std::move(lambda));
__lambda_4_17 copied_lambda = __lambda_4_17(moved_lambda);
using FuncPtr_9 = int (*)(int, int);
FuncPtr_9 direct_func = static_cast<int (*)(int, int)>(copied_lambda.operator __lambda_4_17::retType_4_17());
// lambda(5, 6); // Usage of moved object may lead to UB
moved_lambda(5, 6);
copied_lambda(5, 6);
direct_func(5, 6);
#endif
#ifdef EXAMPLE_7
int number = 4;
class __lambda_5_16 {
public:
inline int operator()(int n) const {
return number + n;
}
private:
int &number;
public:
inline constexpr __lambda_5_16(__lambda_5_16 &&) noexcept = default;
__lambda_5_16(int &_number) : number{_number} {}
};
__lambda_5_16 lambda = __lambda_5_16(__lambda_5_16{number});
// int (*func)(int) = lambda; // Won't compile - no visible conversion
#endif
#ifdef EXAMPLE_8
int number1, number2;
number1 = 1; number2 = 2;
[number1, &number2]() mutable {
number1++;
number2++;
return number1 * number2;
}();
class A {
int n1;
int &n2;
int *n3;
A () : n2(n1), n3(&n1) {}
void func() const {
//n1++;
n2++; // OK
//n3++;
(*n3)++; // OK
}
};
#endif
#elif __cplusplus > 201103L && __cplusplus <= 201402L // C++14
#ifdef EXAMPLE_1
auto lambda = [](auto a, auto b) {
return a + b;
};
lambda(1, 2.4); // int, double
lambda(5.f, 3); // float, int
#endif
#ifdef EXAMPLE_2
int a = 4, k = 7;
auto lambda = [&b = a, k = a]() mutable {
b++;
std::cout << k << std::endl; // Prints: 4
};
lambda();
std::cout << a << std::endl; // Prints: 5
#endif
#ifdef EXAMPLE_3
// generic lambda, operator() is a template with one parameter
auto vglambda = [](auto printer) {
return [=](auto &&... ts) { // generic lambda, ts is a parameter pack
printer(std::forward<decltype(ts)>(ts)...);
};
};
auto triple_printer = vglambda([](auto v1, auto v2, auto v3) { std::cout << v1 << v2 << v3; });
triple_printer(1, 'a', 3.14); // outputs 1a3.14
#endif
#ifdef EXAMPLE_4
__lambda_4_16 lambda = __lambda_4_16(__lambda_4_16{});
lambda.operator()(1, 2.3999999999999999);
lambda.operator()(5.0F, 3);
#endif
#ifdef EXAMPLE_5
#endif
#elif __cplusplus > 201402L && __cplusplus <= 201703L // C++17
#ifdef EXAMPLE_1
struct my_struct {
my_struct() {}
my_struct(const my_struct &) { std::cout << "Copy" << std::endl; } // Called during this->func();
void func() {
[*this]() { // call copy constructor
};
[this] { // doesn't call copy constructor
};
}
};
my_struct ms;
ms.func();
#endif
#ifdef EXAMPLE_2
class lambda_7_3
{
public:
[[nodiscard]] inline int operator()() const
{
std::cout << "A" << std::endl;
return 5;
}
using retType_7_3 = int (*)();
[[nodiscard]] inline constexpr operator retType_7_3 () const noexcept
{
return invoke;
};
private:
static inline int invoke()
{
std::cout << "B" << std::endl;
return 5;
}
};
lambda_7_3 a;
a();
auto c = a;
int (*b)() = a;
b();
c();
#endif
#ifdef EXAMPLE_3
auto Fwd = [](int(*fp)(int), auto a) { return fp(a); };
auto C = [](auto a) { return a; };
static_assert(Fwd(C, 3) == 3); // OK
auto NC = [](auto a) { static int s; return a; };
// static_assert(Fwd(NC, 3) == 3); // Won't compile
#endif
#else // C++20
#ifdef EXAMPLE_1
auto sum = [] <typename T, Numeric U> (T num1, U num2) requires ( std::is_arithmetic_v<T> ) {
return num1 + num2;
};
std::cout << sum(3.2, 5) << std::endl;
#endif
#ifdef EXAMPLE_2
using namespace std::string_literals;
auto plus = [] <typename T> (T arg1, T arg2) {
return arg1 + arg2;
};
std::cout << plus("Hello "s, "World"s) << std::endl;
#endif
#ifdef EXAMPLE_3
using namespace std::string_literals;
auto plus = [] <PlusOperator T, PlusOperator ...Args> (T first, Args ...args) requires ( std::is_same_v<T, Args> && ... ) {
return (first + ... + args);
};
std::cout << plus(1) << std::endl; // Prints: 1
std::cout << plus(1, 4, 5) << std::endl; // Prints: 10
std::cout << plus(2.3, 4.5, 5.6) << std::endl; // Prints: 12.4
std::cout << plus("Template"s, " "s, "Lambda"s, " "s, "Expression"s) << std::endl; // Prints: Template Lambda Expression
#endif
#ifdef EXAMPLE_4
#endif
#endif
return EXIT_SUCCESS;
}