-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.cpp
72 lines (54 loc) · 2.44 KB
/
lambda_function.cpp
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
#include <iostream>
#include <vector>
#include <typeinfo>
#include <array>
using namespace std;
//lambda function
//format: [...](...) -> typeReturn { }
// - [vEnv1, vEnv2, ...] - capture bloc, must be here even if empty
// - (param1, param2, ...) - must be here even if empty
// typeReturn - trailing return type, if function returns a value or compiler cannot find the type automatically
int main()
{
//lambda function
auto computeTaxPrice = [](float noTaxPrice, float taxRate) { return noTaxPrice * (1. + taxRate); };
cout << "type of variable returned " << typeid(computeTaxPrice).name() << endl;
double fullPrice = computeTaxPrice(100, 0.2);
cout << "computed price " << fullPrice << endl;
//definition of a lambda function that captures an environment variable where it is executed
int delta = 3;
auto incByDelta = [delta](int a) { return a + delta; };
cout << "10+" << delta << " = " << incByDelta(10) << endl;
delta = 5;
//lambda function captures the delta value when it is created
//further changes are not taken into account
cout << "10+" << delta << " = " << incByDelta(10) << endl;
//environment variables can be captured by their reference
auto incByRefDelta = [&delta](int a) { return a + delta; };
cout << "10+" << delta << " = " << incByRefDelta(10) << endl;
delta = 7;
//lambda function captures the delta value when it is created
//further changes are not taken into account
cout << "10+" << delta << " = " << incByRefDelta(10) << endl;
// trailing return type example
auto incByRefDelta2 = [&delta](int a) -> float { return a + delta; };
auto result = incByRefDelta2(100);
cout << "type of result " << typeid(result).name() << endl;
// with several environment variables
string name = "Jack";
array<int, 2> tbAges = {5, 7};
// lambda function
auto displayDetails = [&tbAges, &name]() {
cout << name << " has " << tbAges.size() << " children being " << tbAges[0] << " aand "
<< tbAges[1] << " year old." << endl;
};
displayDetails();
//capture options
// [=] - capture everything by copy - modifications after are not seen by the lambda function
// [&] - capture everything by value - modification are seen
//custom captures
// [x, &y] capture x by copy and y by ref
// [&, z] capture all by copy but z by ref
cout << "\n\n normal end of the program" << endl;
return 0;
}