forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.1.cpp
More file actions
55 lines (48 loc) · 1.32 KB
/
3.1.cpp
File metadata and controls
55 lines (48 loc) · 1.32 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
//
// 3.1.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// lambda expression
#include <iostream>
#include <utility>
void learn_lambda_func_1() {
int value_1 = 1;
auto copy_value_1 = [value_1] {
return value_1;
};
value_1 = 100;
auto stored_value_1 = copy_value_1();
std::cout << "stored_value_1=" << stored_value_1 << std::endl;
// 这时, stored_value_1 == 1, 而 value_1 == 100.
// 因为 copy_value_1 在创建时就保存了一份 value_1 的拷贝
}
void learn_lambda_func_2() {
int value_2 = 1;
auto copy_value_2 = [&value_2] {
return value_2;
};
value_2 = 100;
auto stored_value_2 = copy_value_2();
std::cout << "stored_value_2=" << stored_value_2 << std::endl;
// 这时, stored_value_2 == 100, value_2 == 100.
// 因为 copy_value_2 保存的是引用
}
int main() {
learn_lambda_func_1();
learn_lambda_func_2();
auto important = std::make_unique<int>(1);
auto add = [v1 = 1, v2 = std::move(important)](int x, int y) -> int {
return x+y+v1+(*v2);
};
std::cout << add(3,4) << std::endl;
// 泛型 lambda
auto generic = [](auto x, auto y) {
return x+y;
};
generic(1, 2);
generic(1.1, 2.2);
return 0;
}