mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
book: fix an inconsistency between en-us and zh-cn
This commit is contained in:
@@ -105,7 +105,9 @@ The type of the captured variable being declared is judged according to the expr
|
|||||||
and the judgment is the same as using `auto`:
|
and the judgment is the same as using `auto`:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <memory>
|
#include <iostream>
|
||||||
|
#include <memory> // std::make_unique
|
||||||
|
#include <utility> // std::move
|
||||||
|
|
||||||
void lambda_expression_capture() {
|
void lambda_expression_capture() {
|
||||||
auto important = std::make_unique<int>(1);
|
auto important = std::make_unique<int>(1);
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ void lambda_reference_capture() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**3. 隐式捕获**
|
#### 3. 隐式捕获
|
||||||
|
|
||||||
手动书写捕获列表有时候是非常复杂的,这种机械性的工作可以交给编译器来处理,这时候可以在捕获列表中写一个
|
手动书写捕获列表有时候是非常复杂的,这种机械性的工作可以交给编译器来处理,这时候可以在捕获列表中写一个
|
||||||
`&` 或 `=` 向编译器声明采用引用捕获或者值捕获.
|
`&` 或 `=` 向编译器声明采用引用捕获或者值捕获.
|
||||||
@@ -79,7 +79,7 @@ void lambda_reference_capture() {
|
|||||||
- \[&\] 引用捕获, 让编译器自行推导引用列表
|
- \[&\] 引用捕获, 让编译器自行推导引用列表
|
||||||
- \[=\] 值捕获, 让编译器自行推导值捕获列表
|
- \[=\] 值捕获, 让编译器自行推导值捕获列表
|
||||||
|
|
||||||
**4. 表达式捕获**
|
#### 4. 表达式捕获
|
||||||
|
|
||||||
> 这部分内容需要了解后面马上要提到的右值引用以及智能指针
|
> 这部分内容需要了解后面马上要提到的右值引用以及智能指针
|
||||||
|
|
||||||
@@ -93,13 +93,12 @@ C++14 给与了我们方便,允许捕获的成员用任意的表达式进行
|
|||||||
#include <memory> // std::make_unique
|
#include <memory> // std::make_unique
|
||||||
#include <utility> // std::move
|
#include <utility> // std::move
|
||||||
|
|
||||||
int main() {
|
void lambda_expression_capture() {
|
||||||
auto important = std::make_unique<int>(1);
|
auto important = std::make_unique<int>(1);
|
||||||
auto add = [v1 = 1, v2 = std::move(important)](int x, int y) -> int {
|
auto add = [v1 = 1, v2 = std::move(important)](int x, int y) -> int {
|
||||||
return x+y+v1+(*v2);
|
return x+y+v1+(*v2);
|
||||||
};
|
};
|
||||||
std::cout << add(3,4) << std::endl;
|
std::cout << add(3,4) << std::endl;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user