File tree 2 files changed +6
-5
lines changed
2 files changed +6
-5
lines changed Original file line number Diff line number Diff line change @@ -105,7 +105,9 @@ The type of the captured variable being declared is judged according to the expr
105
105
and the judgment is the same as using ` auto ` :
106
106
107
107
``` cpp
108
- #include < memory>
108
+ #include < iostream>
109
+ #include < memory> // std::make_unique
110
+ #include < utility> // std::move
109
111
110
112
void lambda_expression_capture () {
111
113
auto important = std::make_unique<int>(1);
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ void lambda_reference_capture() {
67
67
}
68
68
```
69
69
70
- ** 3. 隐式捕获**
70
+ #### 3. 隐式捕获
71
71
72
72
手动书写捕获列表有时候是非常复杂的,这种机械性的工作可以交给编译器来处理,这时候可以在捕获列表中写一个
73
73
` & ` 或 ` = ` 向编译器声明采用引用捕获或者值捕获.
@@ -79,7 +79,7 @@ void lambda_reference_capture() {
79
79
- \[ &\] 引用捕获, 让编译器自行推导引用列表
80
80
- \[ =\] 值捕获, 让编译器自行推导值捕获列表
81
81
82
- ** 4. 表达式捕获**
82
+ #### 4. 表达式捕获
83
83
84
84
> 这部分内容需要了解后面马上要提到的右值引用以及智能指针
85
85
@@ -93,13 +93,12 @@ C++14 给与了我们方便,允许捕获的成员用任意的表达式进行
93
93
#include < memory> // std::make_unique
94
94
#include < utility> // std::move
95
95
96
- int main () {
96
+ void lambda_expression_capture () {
97
97
auto important = std::make_unique<int>(1);
98
98
auto add = [v1 = 1, v2 = std::move(important)](int x, int y) -> int {
99
99
return x+y+v1+(*v2);
100
100
};
101
101
std::cout << add(3,4) << std::endl;
102
- return 0;
103
102
}
104
103
```
105
104
You can’t perform that action at this time.
0 commit comments