diff --git a/book/en-us/03-runtime.md b/book/en-us/03-runtime.md index b11c9bb..907c112 100644 --- a/book/en-us/03-runtime.md +++ b/book/en-us/03-runtime.md @@ -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`: ```cpp -#include +#include +#include // std::make_unique +#include // std::move void lambda_expression_capture() { auto important = std::make_unique(1); diff --git a/book/zh-cn/03-runtime.md b/book/zh-cn/03-runtime.md index 7752c9c..debfd6e 100644 --- a/book/zh-cn/03-runtime.md +++ b/book/zh-cn/03-runtime.md @@ -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 // std::make_unique #include // std::move -int main() { +void lambda_expression_capture() { auto important = std::make_unique(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; - return 0; } ```