Professional C__ - Marc Gregoire [224]
Otherwise the return_type is void.
The following example demonstrates that you can create a lambda expression and immediately execute it. The line defines a lambda expression without return type and without any parameters. It simply prints the string “Hello from Lambda” to the console. Note the parentheses () at the end, which causes the lambda to be executed immediately:
[]{cout << "Hello from Lambda" << endl;}();
Code snippet from Lambdas\LambdaInvocation.cpp
The output is as follows:
Hello from Lambda
The following example defines a lambda that accepts a string argument and returns a string. The result is stored in the variable result. Again notice the parentheses at the end of the lambda causing the lambda to be executed immediately:
string result = [](const string& str) -> string {return "Hello from "
+ str;}("second Lambda");
cout << "Result: " << result << endl;
Code snippet from Lambdas\LambdaInvocation.cpp
The output is as follows:
Result: Hello from second Lambda
As mentioned before, the return type can be omitted in this case:
string result = [](const string& str){return "Hello from "
+ str;}("second Lambda");
Code snippet from Lambdas\LambdaInvocation.cpp
You can also store a pointer to a lambda expression and execute the lambda through the function pointer. Using the C++11 auto keyword, this becomes very easy:
auto fn = [](const string& str){return "Hello from " + str;};
cout << fn("call 1") << endl;
cout << fn("call 2") << endl;
Code snippet from Lambdas\LambdaFunctionPointer.cpp
The preceding code results in the following output:
Hello from call 1
Hello from call 2
Capture Block
The square brackets part is called the lambda capture block. It allows you to specify how you want to capture variables from the enclosing scope. Capturing a variable means that the variable becomes available inside the body of the lambda. There are two ways to capture all variables from the enclosing scope:
[=] captures all variables by value
[&] captures all variables by reference
Specifying an empty capture block [] means that no variables from the enclosing scope are being captured. It is also possible to selectively decide which variables to capture and how, by specifying a capture list with an optional capture default. Variables prefixed with & are captured by reference. Variables without a prefix are captured by value. The capture default should be the first element in the capture list and be either & or =. For example:
[&x] captures only x by reference and nothing else.
[x] captures only x by value and nothing else.
[=, &x, &y] captures by value by default, except variables x and y, which are captured by reference.
[&, x] captures by reference by default, except variable x, which is captured by value.
[&x, &x] is illegal because identifiers cannot be repeated.
When you capture a variable by reference, you have to make sure that the reference is still valid at the time the lambda expression is executed. This will be demonstrated with the multiplyBy2Lambda() example in the following section.
Lambda Expressions as Return Type
std::function defined in the function By using std::function, lambda expressions can be returned from functions. Take a look at the following definition: function { return [=]()->int{return 2*x;}; } Code snippet from Lambdas\multiplyBy2Lambda.cpp In this example, the return type and empty parameter list of the lambda expression can be omitted, so the preceding can be written as follows: function