Lambda Expression Syntax
Visual Studio 2010
This topic describes the syntax of lambda expressions. It provides an example that demonstrates the structural elements of a lambda expression and how these elements relate to the example.
The following program uses lambda expressions with two STL algorithms: generate_n and for_each. The lambda expression that appears in the call to the generate_n function assigns an element of a vector object to the sum of the previous two elements. The lambda expression that appears in the call to the for_each function prints an element of the same vector object to the console.
Copy
// lambda_structure.cpp
// compile with: /EHsc
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// The number of elements in the vector.
const int elementCount = 9;
int main()
{
// Create a vector object with each element set to 1.
vector<int> v(elementCount, 1);
// These variables hold the previous two elements of the vector.
int x = 1;
int y = 1;
// Assign each element in the vector to the sum of the
// previous two elements.
generate_n(v.begin() + 2, elementCount - 2, [=]() mutable throw() -> int {
// Generate current value.
int n = x + y;
// Update previous two values.
x = y;
y = n;variable used in lambda
return n;
});
// Print the contents of the vector.
for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });
cout << endl;
// Print the local variables x and y.
// The values of x and y hold their initial values because
// they are captured by value.
cout << x << " " << y << endl;
}
For more information about the generate_n function, see generate_n. For more information about the for_each function, see for_each.
The following sections describe the grammar of a lambda expression and how each element relates to the preceding example.
Lambda Expression Grammar
The following formal definition shows the grammar, in BNF format, of a lambda expression:
lambda-expression
: lambda-introducer lambda-parameter-declarationopt compound-statement
lambda-introducer
: [ lambda-captureopt ]
lambda-capture
: capture-default
| capture-list
| capture-default , capture-list
capture-default
: &
| =
capture-list
: capture
| capture-list , capture
capture
: identifier
| & identifier
| this
lambda-parameter-declaration
: ( lambda-parameter-declaration-listopt ) mutable-specificationopt exception-specificationopt lambda-return-type-clauseopt
lambda-parameter-declaration-list
: lambda-parameter
| lambda-parameter , lambda-parameter-declaration-list
lambda-parameter
: decl-specifier-seq declarator
lambda-return-type-clause
: -> type-id
The following section describes how the grammar relates to the example in the introduction.
Properties of Lambda Expressions
The following illustration maps the grammar to the example.
The callouts in the illustration are as follows:
1. lambda-introducer (referred to as capture clause later in this topic)
2. lambda-parameter-declaration-list (referred to as parameter list later in this topic)
3. mutable-specification (referred to as mutable specification later in this topic)
4. exception-specification (referred to as exception specification later in this topic)
5. lambda-return-type-clause (referred to as return type later in this topic)
6. compound-statement (referred to as lambda body later in this topic)
The following sections describe the grammar in more detail.
Capture Clause
A lambda expression can access any variable that has automatic storage duration and that can be accessed in the enclosing scope. The capture clause specifies whether the body of the lambda expression accesses variables in the enclosing scope by value or by reference: variables that have the ampersand (&) prefix are accessed by reference and variables that do not have the & prefix are accessed by value. The empty capture clause,
[], indicates that the body of the lambda expression accesses no variables in the enclosing scope.
The default capture mode specifies whether capture variables that you do not explicitly specify are captured by value or by reference. You can specify the default capture mode (capture-default in the syntax) by specifying & or = as the first element of the capture clause. The & element specifies that the body of the lambda expression accesses all captured variables by reference unless you explicitly specify otherwise. The = element specifies that the body of the lambda expression accesses all captured variables by value unless you explicitly specify otherwise. For example, if the body of a lambda expression accesses the external variable total by reference and the external variable factor by value, then the following capture clauses are equivalent:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论