-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.cpp
More file actions
99 lines (76 loc) · 2.69 KB
/
lambda.cpp
File metadata and controls
99 lines (76 loc) · 2.69 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
/*
Lambda expression function is similar to anonymous function.
Basic syntax of C++11 Lambda expression:
[capture list] (parameter list) mutable exception attribute -> return type { function body; }
Not all of them are mandatory, in actual using, some can be excluded.
For example, belows are frrequent usage:
-> [capture list] (params list) -> return type {function body}
-> [capture list] (params list) {function body}
-> [capture list] {function body}
Capture list can be below types:
1. Value
2. Reference
3. Implicit
4. Expression
For 1, 2, 3, capture the variables that already defined in the outer scope
Lambda is a class type (similar to function object type) essentially.
So lambda also can be called as a function type. --------------------------- see std::function for more details.
*/
#include <iostream>
#include <memory>
using namespace std;
void lambda_capture_value()
{
int val = 1;
auto copy_val = [val] { return val; };
// Implicit capture, value capture
//auto copy_val = [=] { return val; };
val = 100;
auto saved_val = copy_val();
// value is 1, it is copied when the lambda function is created
cout << "After capture value, saved value is " << saved_val << endl;
};
void lambda_capture_reference()
{
int val = 1;
auto copy_val = [&val] { return val; };
// Implicit capture, reference capture
//auto copy_val = [&] { return val; };
val = 100;
auto saved_val = copy_val();
// value is 100, bscause it is already passed by reference
cout << "After capture reference, saved value is " << saved_val << endl;
};
void lambda_capture_expression()
{
auto important = make_unique<int>(1); // after created, important is exclusive pointer, refer to unique_ptr in C++11, can't be moved or copied
auto add = [v1 = 1, v2 = move(important)](int a, int b) -> int { return a + b + v1 + (*v2); };
cout << "After capture expression, sum value is " << add(3, 4) << endl;
};
void lambda_generic()
{
auto generic = [](auto a, auto b) { return a + b; };
cout << "generic lambda -> " << generic(1, 2) << endl;
cout << "generic lambda -> " << generic(1.1, 2.2) << endl;
};
// see std::function
using funcPtr = void(int);
void func(funcPtr f)
{
f(1);
};
void lambda_call_as_function_object()
{
auto f = [](int val) { cout << "Value is " << val << " after calling this lambda as function object." << endl;};
func(f); // calling f function pointer
f(1); // calling f by lambda
};
int main()
{
lambda_capture_value();
lambda_capture_reference();
lambda_capture_expression();
lambda_generic();
lambda_call_as_function_object();
return 0;
}