Guide
syntax
syntax
[ capture clause ] (parameters) -> return-type
{
definition of method
}
capture
We can capture external variables from enclosing scope by three ways :
Capture by reference
Capture by value (making a copy)
Capture by both (mixed capture)
Syntax used for capturing variables :
[]: capture nothing
[&] : capture all external variable by reference
[=] : capture all external variable by value (making a copy)
[a, &b] : capture a by value and b by reference
[this] : Capture the this pointer of the enclosing class
C++11中的Lambda表达式捕获外部变量主要有以下形式:
捕获形式 说明
[] 不捕获任何外部变量
[变量名, …] 默认以值得形式捕获指定的多个外部变量(用逗号分隔),如果引用捕获,需要显示声明(使用&说明符)
[this] 以值的形式捕获this指针
[=] 以值的形式捕获所有外部变量
[&] 以引用形式捕获所有外部变量
[=, &x] 变量x以引用形式捕获,其余变量以传值形式捕获
[&, x] 变量x以值的形式捕获,其余变量以引用形式捕获
example code
1 |
|
Reference
- c++11-lambda-closures
- c-lambda-function-tutorial-lambda-expression-in-c-example
- lambda-expression-in-c
History
- 20180823: created.