-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGenericFunctions.cpp
More file actions
579 lines (453 loc) · 16.4 KB
/
GenericFunctions.cpp
File metadata and controls
579 lines (453 loc) · 16.4 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// =====================================================================================
// GenericFunctions.cpp // Generic Functions (incl. Lambdas)
// =====================================================================================
module modern_cpp:generic_functions;
namespace GenericFunctions {
// ---------------------------------------------------------------------------------
// generic function
static void function(auto x, int y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
};
static void test_01()
{
function(1, 123);
function(2.5, 123);
function(std::string{ "ABC" }, 123);
// compiles too
function<int>(1, 123);
function<double>(2.5, 123);
function<std::string>(std::string{ "ABC" }, 123);
}
// ---------------------------------------------------------------------------------
// generic function - how the compiler sees them
template<typename T>
static void Function(T x, int y)
{
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
void Function<int>(int x, int y)
{
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
void Function<double>(double x, int y)
{
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
void Function<std::string>(std::string x, int y)
{
std::cout << "x=" << x << ", y=" << y << std::endl;
}
static void test_02()
{
Function<int>(1, 123);
Function<double>(123.456, 123);
Function<std::string>(std::string{ "ABC" }, 123);
}
// ---------------------------------------------------------------------------------
// generic function - several 'auto' parameters
static void functionTwice(auto x, auto y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
};
// ---------------------------------------------------------------------------------
// generic function - several 'auto' parameters - how the compiler sees them
template<typename T, typename U>
static void FunctionTwice(T x, U y)
{
std::cout << "x=" << x << ", y=" << y << std::endl;
}
static void test_03()
{
functionTwice(1, 2);
functionTwice(123l, 456ll);
functionTwice(123.456, 123.456F);
FunctionTwice(1, 2);
FunctionTwice(123l, 456ll);
FunctionTwice(123.456, 123.456F);
FunctionTwice<int, int>(1, 2);
FunctionTwice<long, long long>(123l, 456ll);
FunctionTwice<double, float>(123.456, 123.456F);
}
}
// -------------------------------------------------------------------------------------
namespace GenericLambdas {
// ---------------------------------------------------------------------------------
// generic lambda
auto lambda = [](auto x, int y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
};
static void test_01()
{
lambda(1, 123);
lambda(2.5, 123);
lambda(std::string{ "ABC" }, 123);
}
// ---------------------------------------------------------------------------------
// generic lambda - how the compiler sees them
struct Lambda
{
template <typename T>
auto operator() (T x, int y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
auto operator() <int> (int x, int y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
auto operator() <double> (double x, int y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
auto operator() <std::string> (std::string x, int y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
};
static void test_02()
{
struct Lambda instance;
instance(1, 200);
instance(2.5, 201);
instance(std::string{ "ABC" }, 202);
// compiles too
instance.operator()<int>(1, 200);
instance.operator()<double>(2.5, 201);
instance.operator()<std::string>(std::string{ "ABC" }, 202);
}
// ---------------------------------------------------------------------------------
// generic lambda - several 'auto' parameters
auto lambdaTwice = [](auto x, auto y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
};
// ---------------------------------------------------------------------------------
// generic lambda - several 'auto' parameters - how the compiler sees them
struct LambdaTwice
{
template <typename T, typename U>
auto operator() (T x, U y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
auto operator() <int,int> (int x, int y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
auto operator() <long, long long> (long x, long long y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
template<>
auto operator() <double, float> (double x, float y) {
std::cout << "x=" << x << ", y=" << y << std::endl;
}
};
static void test_03()
{
lambdaTwice(1, 2);
lambdaTwice(123l, 456ll);
lambdaTwice(123.456, 123.456F);
struct LambdaTwice instance;
instance(1, 2);
instance(123l, 456ll);
instance(123.456, 123.456F);
// compiles too
instance.operator()<int, int>(1, 2);
instance.operator()< long, long long >(123l, 456ll);
instance.operator()<double, float>(123.456, 123.456F);
}
}
// -------------------------------------------------------------------------------------
namespace GenericLambdasExample {
static void test_01()
{
// define a generic lambda
auto isGreaterThanFifty = [](auto n) { return n > 50; };
std::vector<int> intValues{ 44, 65, 22, 77, 2 };
// use generic lambda with a vector of integers
auto it1 = std::find_if(
intValues.begin(),
intValues.end(),
isGreaterThanFifty
);
if (it1 != intValues.end()) {
std::cout << "Found a value: " << *it1 << std::endl;
}
std::vector<double> doubleValues{ 24.5, 75.5, 12.5, 87.5, 12.5 };
// use exactly the *same* generic lambda with a vector of doubles
auto it2 = std::find_if(
doubleValues.begin(),
doubleValues.end(),
isGreaterThanFifty
);
if (it2 != doubleValues.end()) {
std::cout << "Found a value: " << *it2 << std::endl;
}
}
template <typename T>
bool isGreaterThanFiftyEx(const T& n)
{
return n > 50;
};
static void test_02()
{
std::vector<int> intValues{ 44, 65, 22, 77, 2 };
// use template function with a vector of integers
auto it1 = std::find_if(
intValues.begin(),
intValues.end(),
isGreaterThanFiftyEx<int>
);
if (it1 != std::end(intValues)) {
std::cout << "Found a value: " << *it1 << std::endl;
}
std::vector<double> doubleValues{ 24.5, 75.5, 12.5, 87.5, 12.5 };
// use exactly the *same* template function with
// another specialization with a vector of doubles
auto it2 = std::find_if(
doubleValues.begin(),
doubleValues.end(),
isGreaterThanFiftyEx<double>
);
if (it2 != std::end(doubleValues)) {
std::cout << "Found a value: " << *it2 << std::endl;
}
}
}
// -------------------------------------------------------------------------------------
namespace GenericFunctionsExample {
// define a generic function (top-level (!))
static auto isGreaterThanFifty(auto n) { return n > 50; };
static void test_01()
{
std::vector<int> intValues{ 44, 65, 22, 77, 2 };
// use generic function with a vector of integers
auto it1{ std::find_if(
intValues.begin(),
intValues.end(),
isGreaterThanFifty<int>
) };
if (it1 != intValues.end()) {
std::cout << "Found a value: " << *it1 << std::endl;
}
std::vector<double> doubleValues{ 24.5, 75.5, 12.5, 87.5, 12.5 };
// use exactly the *same* generic function with a vector of doubles
auto it2{ std::find_if(
doubleValues.begin(),
doubleValues.end(),
isGreaterThanFifty<double>
) };
if (it2 != doubleValues.end()) {
std::cout << "Found a value: " << *it2 << std::endl;
}
}
}
// -------------------------------------------------------------------------------------
namespace GenericTemplatedLambdas {
static void test_01()
{
auto l1 = [](int a) { return a + a; }; // C++ 11, regular lambda
auto l2 = [](auto a) { return a + a; }; // C++ 14, generic lambda
auto l3 = []<typename T>(T a) { return a + a; }; // C++ 20, template lambda
auto v1 = l1(42); // Ok
// auto v2 = l1(42.0); // Warning
// auto v3 = l1(std::string{ "42" }); // Error
auto v5 = l2(42); // Ok
auto v6 = l2(42.0); // Ok
auto v7 = l2(std::string{ "42" }); // Ok
auto v8 = l3(42); // Ok
auto v9 = l3(42.0); // Ok
auto v10 = l3(std::string{ "42" }); // Ok
}
static void test_02()
{
auto l1 = [](int a, int b) { return a + b; }; // C++ 11, regular lambda
auto l2 = [](auto a, auto b) { return a + b; }; // C++ 14, generic lambda
auto l3 = []<typename T, typename U>(T a, U b) { // C++ 20, template lambda
return a + b;
};
auto v1 = l1(42, 1); // Ok
// auto v2 = l1(42.0, 1.0); // Warning
// auto v3 = l1(std::string{ "42" }, '1'); // Error
auto v4 = l2(42, 1); // Ok
auto v5 = l2(42.0, 1); // Ok
auto v6 = l2(std::string{ "42" }, '1'); // Ok
auto v7 = l2(std::string{ "42" }, std::string{ "1" }); // Ok
auto v8 = l3(42, 1); // Ok
auto v9 = l3(42.0, 1); // Ok
auto v10 = l3(std::string{ "42" }, '1'); // Ok
auto v11 = l3(std::string{ "42" }, std::string{ "42" }); // Ok
}
static void test_03()
{
auto l5 = []<typename T>(T a, T b) { return a + b; }; // C++ 20, template lambda
auto v1 = l5(42, 1); // Ok
// auto v2 = l5(42, 1.0); // Error
auto v4 = l5(42.0, 1.0); // Ok
// auto v5 = l5(42, false); // Error
auto v6 = l5(std::string{ "42" }, std::string{ "1" }); // Ok
// auto v6 = l5(std::string{ "42" }, '1'); // Error
}
static void test_04()
{
auto l6 = [](auto a, decltype(a) b) { return a + b; }; // C++ 14, using decltype
auto v1 = l6(42.0, 1); // Ok
// auto v2 = l6(42, 1.0); // Warning
// auto v3 = l6(std::string{ "42" }, '1'); // Error
}
// -------------------------------------------------------------------------------------
static void foo(const std::string& s) {
std::cout << "Signature: const&" << std::endl;
}
static void foo(std::string&& s) {
std::cout << "Signature: &&" << std::endl;
}
auto callingFoo = [](auto&& s) {
std::cout << "Calling foo(): " << s;
foo(std::forward<decltype(s)>(s));
};
static void test_05()
{
const std::string str{ "Hello World with LValue - " };
callingFoo(str);
callingFoo("Hello World with RValue - ");
}
}
// -------------------------------------------------------------------------------------
namespace GenericRecursiveLambdas {
static void test_01()
{
// power recursive function
std::function<int(int, int)> power;
power = [&](int base, int exp) {
return exp == 0 ? 1 : base * power(base, exp - 1);
};
std::cout << power(2, 10) << std::endl; // 2^10 = 1024
}
static void test_02()
{
// factorial recursive function
std::function<int(int)> factorial;
factorial = [&](int n) {
if (n < 2) {
return 1;
}
else {
return n * factorial(n - 1);
}
};
std::cout << factorial(5) << std::endl; // 120
}
static void test_03()
{
// power recursive lambda function
auto power = [](auto self, auto base, int exp) -> decltype(base) {
return exp == 0 ? 1 : base * self(self, base, exp - 1);
};
std::cout << power(power, 2, 10) << std::endl; // 2^10 = 1024
std::cout << power(power, 2.71828, 10); // e^10 = 22026.3
}
static void test_04()
{
// factorial recursive lambda function
auto factorial = [](auto f, int const n) {
if (n < 2) {
return 1;
}
else {
return n * f(f, n - 1);
}
};
std::cout << factorial(factorial, 5) << std::endl; // 120
}
}
// -------------------------------------------------------------------------------------
namespace GenericLambdasCurrying {
static void test_01() {
// Example demonstrating so called 'Currying':
// This means that we take a function that can accept some parameters
// and store it in another function object, which accepts *fewer* parameters.
// In our example, we define a 'plusTen' function which accepts a single parameter.
// This parameter is forwarded to the 'plus' function.
// The second parameter equals 10, which is being saved in the function object:
auto plus = [](auto l, auto r) { return l + r; };
auto plusTen = [plus](auto x) { return plus(10, x); };
std::cout << plusTen(5) << std::endl;
}
// ---------------------------------------------------------------------
// correlations between templates and lambdas
template <typename T, typename U>
auto add = [](const T& t, const U& u) -> decltype (t + u)
{
return t + u;
};
static void test_02() {
int n = 1;
double d = 2.7;
auto result1 = add<int, double>(n, d);
std::cout << result1 << std::endl;
}
}
// -------------------------------------------------------------------------------------
static void test_generic_functions()
{
using namespace GenericFunctions;
test_01();
test_02();
test_03();
}
static void test_generic_functions_example()
{
using namespace GenericFunctionsExample;
test_01();
}
static void test_generic_lambdas()
{
using namespace GenericLambdas;
test_01();
test_02();
test_03();
}
static void test_generic_lambdas_example()
{
using namespace GenericLambdasExample;
test_01();
test_02();
}
static void test_templated_lambdas()
{
using namespace GenericTemplatedLambdas;
test_01();
test_02();
test_03();
test_04();
test_05();
}
static void test_recursive_lambdas()
{
using namespace GenericRecursiveLambdas;
test_01();
test_02();
test_03();
test_04();
}
static void test_lambdas_and_currying()
{
using namespace GenericLambdasCurrying;
test_01();
test_02();
}
// -------------------------------------------------------------------------------------
void main_generic_functions()
{
test_generic_functions();
test_generic_functions_example();
test_generic_lambdas();
test_generic_lambdas_example();
test_templated_lambdas();
test_recursive_lambdas();
test_lambdas_and_currying();
}
// =====================================================================================
// End-of-File
// =====================================================================================