- Learning C++ Functional Programming
- Wisnu Anggoro
- 220字
- 2021-07-02 20:51:38
Preparing the value using initialization captures
Another great feature of the Lambda expression coming up in C++14 is its initialization captures. The expression can capture a value of the variable and assign it to the expression's variable. Let's take a look at the following piece of code implementing the initialization captures:
/* lambda_initialization_captures.cpp */
#include <iostream>
using namespace std;
auto main() -> int
{
cout << "[lambda_initialization_captures.cpp]" << endl;
// Initializing a variable
int a = 5;
cout << "Initial a = " << a << endl;
// Initializing value to lambda using the variable
auto myLambda = [&x = a]() { x += 2; };
// Executing the Lambda
myLambda();
// Displaying a new value of the variable
cout << "New a = " << a << endl;
return 0;
}
As we can see in the preceding code, we have an int variable named a with the value 5. The Lambda expression, myLambda, then captures the a value and executes it in the code. The result is that now the a value will be 7 since it is added by 2. The following output screenshot should appear in our console window when we run the preceding code:

From the preceding snapshot, we see that we can prepare the value to be included in the calculation inside the Lambda expression.
- JavaScript Unlocked
- Java Web開發之道
- 羅克韋爾ControlLogix系統應用技術
- Java Web程序設計
- 深度學習:算法入門與Keras編程實踐
- Web程序設計(第二版)
- Nexus規模化Scrum框架
- Android應用案例開發大全(第二版)
- Julia for Data Science
- Go語言開發實戰(慕課版)
- PHP 8從入門到精通(視頻教學版)
- Mastering PowerCLI
- Implementing Domain:Specific Languages with Xtext and Xtend
- 樹莓派開發從零開始學:超好玩的智能小硬件制作書
- Python程序設計:基礎與實踐