-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_example.cpp
More file actions
57 lines (40 loc) · 839 Bytes
/
lambda_example.cpp
File metadata and controls
57 lines (40 loc) · 839 Bytes
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
#include <tuple>
#include <iostream>
#include <functional>
using namespace std;
void callFunction(std::function<void(int)> f) {
f(5);
}
void test1() {
auto f = [] (int param) {
cout << "param=" << param << endl;
};
callFunction(f);
}
class MyClass {
public:
MyClass() {
cout << "constructor" << endl;
}
MyClass(const MyClass &other) {
cout << "copy constructor" << endl;
}
};
void test2() {
MyClass object1;
int i = 6;
auto lambdaWithCapture = [object1, i](int param) {
cout << "param=" << param << "; i=" << i << endl;
};
i = 10;
callFunction(lambdaWithCapture);
auto lambdaWithReferenceCapture = [&object1, &i](int param) {
cout << "param=" << param << "; i=" << i << endl;
};
i = 15;
callFunction(lambdaWithReferenceCapture);
}
int main(int argc, const char * *argv) {
test1();
test2();
}