Lambda 表达式语法

本文演示了 lambda 表达式的语法和结构化元素。 有关 lambda 表达式的说明,请参阅 C++ 中的 Lambda 表达式

Lambda 表达式语法

ISO C++11 标准中的以下定义显示了 lambda 表达式的语法。 (使用 opt 下标的项是可选项。)

lambda-introducer lambda-declaratoropt compound-statement

将进一步指定以下语法要素:

lambda-introducer:
[ lambda-captureopt ]
lambda-capture:
capture-default
capture-list
capture-default , capture-list
capture-default:
&
=
capture-list:
capture ...opt
capture-list , capture ...opt
capture:
identifier
& identifier
this
lambda-declarator:
( parameter-declaration-clause ) mutableopt
exception-specificationopt attribute-specifier-seqopt trailing-return-typeopt

Visual Studio 支持 C++11 标准 lambda 表达式语法和功能,以下功能除外:

  • 像所有其他类一样,lambda 不会获得自动生成的移动构造函数和移动赋值运算符。 有关右值引用行为支持的详细信息,请参阅对 C++11 功能的支持(现代 C++)中的“右值引用”部分。

  • 此版本不支持可选的 attribute-specifier-seq。

除了 C++11 标准 lambda 功能之外,Visual Studio 还包括以下功能:

  • 无状态 lambda,可完全转换为使用任意调用约定的函数指针。

  • 只要所有返回语句具有相同的类型,就会自动推导比 { return expression; } 更复杂的 lambda 主体的返回类型。 (此功能是拟建的 C++14 标准的一部分。)

Lambda 表达式的属性

下图将该语法映射到示例:

Structural elements of a lambda expression

  1. lambda-introducer(也称为 capture 子句)

  2. lambda declarator(也称为参数列表)

  3. mutable(也称为可变规范)

  4. exception-specification(也称为异常规范)

  5. trailing-return-type(也称为返回类型)

  6. compound-statement(也称为 lambda 体)

Capture 子句

一个 lambda 表达式实质上是一个类、构造函数和函数调用运算符。 就像你定义类时一样,在 lambda 中你必须要决定生成的对象是通过值还是引用捕获变量,或者根本不捕获。 如果一个 lambda 表达式必须访问局部变量和函数参数,则必须捕获它们。 capture 子句(标准语法中的 lambda-introducer)指定 lambda 表达式的主体通过值还是引用访问封闭范围中的变量。 有与号 (&) 前缀的变量通过引用访问,没有该前缀的变量通过值访问。

空 capture 子句 [ ] 指示 lambda 表达式的主体不访问封闭范围中的变量。

使用默认捕获模式(标准语法中的 capture-default)以通过值或引用捕获未指定的变量。 通过将 & 或 = 用作 capture 子句的第一个元素来指定默认捕获模式。 & 元素指示 lambda 表达式的主体通过引用访问未指定的变量。 = 元素指示 lambda 表达式的主体通过值访问未指定的变量。 例如,如果 lambda 体通过引用访问外部变量 total 并通过值访问外部变量 factor,则以下 capture 子句等效:

[&total, factor]
[factor, &total]
[&, factor]
[factor, &]
[=, &total]
[&total, =]

关于 capture-default 的一种常见的误解是,范围内的所有变量无论是否在 lambda 中使用,都会被捕获。 事实上并非如此 - 使用 capture-default 时,只有 lambda 中提及的变量才会被捕获。

如果 capture 子句包含 capture-default &,则该 capture 子句的 capture 中没有任何 identifier 可采用 & identifier 形式。 同样,如果 capture 子句包含 capture-default =,则该 capture 子句的 capture 不能采用 = identifier 形式。 identifier 或 this 在 capture 子句中出现的次数不能超过一次。 以下代码片段给出了一些示例。

struct S { void f(int i); };

void S::f(int i) {
    [&, i]{};    // OK
    [&, &i]{};   // ERROR: i preceded by & when & is the default
    [=, this]{}; // ERROR: this when = is the default
    [i, i]{};    // ERROR: i repeated
}

capture 后跟省略号是包扩展,如以下可变参数模板示例中所示:

template<class... Args>
void f(Args... args) {
    auto x = [args...] { return g(args...); };
    x();
}

可以将 lambda 表达式用于类方法的主体中。 将 this 指针传递到 capture 子句以提供对封闭类的方法和数据成员的访问权限。 有关显示如何使用具有类方法的 lambda 表达式的示例,请参阅 Lambda 表达式的示例中的“示例:在方法中使用 Lambda 表达式”。

在使用 capture 子句时,建议你记住以下几点(尤其是使用采取多线程的 lambda 时):

  • 引用捕获可用于修改外部变量,而值捕获却不能实现此操作。 (mutable 允许修改副本,而不能修改原始项。)

  • 引用捕获会反映外部变量的更新,而值捕获却不会反映。

  • 引用捕获引入生存期依赖项,而值捕获却没有生存期依赖项。

参数列表

参数列表(标准语法中的 lambda declarator)是可选的,它类似于函数的参数列表。

lambda 表达式可以将另一个 lambda 表达式作为其参数。 有关详细信息,请参阅主题 Lambda 表达式的示例中的“高阶 Lambda 表达式”。

由于参数列表是可选的,因此在不将参数传递到 lambda 表达式,并且其 lambda-declarator: 不包含 exception-specification、trailing-return-type 或 mutable 的情况下,可以省略空括号。

可变规范

通常,lambda 的函数调用运算符为 const-by-value,但对 mutable 关键字的使用可将其取消。 它不会生成可变的数据成员。 利用可变规范,lambda 表达式的主体可以修改通过值捕获的变量。 本文后面的一些示例将显示如何使用 mutable。

异常规范

你可以使用 throw() 异常规范来指示 lambda 表达式不会引发任何异常。 与普通函数一样,如果 lambda 表达式声明 throw() 异常规范且 lambda 体引发异常,Visual C++ 编译器将生成警告 C4297,如下所示:

// throw_lambda_expression.cpp
// compile with: /W4 /EHsc
int main() // C4297 expected
{
   []() throw() { throw 5; }();
}

有关详细信息,请参阅异常规范

返回类型

将自动推导 lambda 表达式的返回类型。 无需表示 auto 关键字,除非指定 trailing-return-type。 trailing-return-type 类似于普通方法或函数的返回类型部分。 但是,返回类型必须跟在参数列表的后面,你必须在返回类型前面包含 trailing-return-type 关键字 ->。

如果 lambda 体仅包含一个返回语句或其表达式不返回值,则可以省略 lambda 表达式的返回类型部分。 如果 lambda 体包含单个返回语句,编译器将从返回表达式的类型推导返回类型。 否则,编译器会将返回类型推导为 void。 下面的代码示例片段说明了这一原则。

auto x1 = [](int i){ return i; }; // OK: return type is int
auto x2 = []{ return{ 1, 2 }; };  // ERROR: return type is void, deducing
                                  // return type from braced-init-list is not valid

lambda 表达式可以生成另一个 lambda 表达式作为其返回值。 有关详细信息,请参阅 Lambda 表达式的示例中的“高阶 Lambda 表达式”。

Lambda 体

lambda 表达式的 lambda 体(标准语法中的 compound-statement)可包含普通方法或函数的主体可包含的任何内容。 普通函数和 lambda 表达式的主体均可访问以下变量类型:

  • 参数

  • 本地声明变量

  • 类数据成员(在类内部声明并且捕获 this 时)

  • 具有静态存储持续时间的任何变量(例如,全局变量)

此外,lambda 表达式可以访问它从封闭范围中捕获的变量。 如果某个变量显示在 lambda 表达式的 capture 子句中,则该变量是显式捕获的。 否则,该变量是隐式捕获的。 lambda 表达式的主体使用默认捕获模式来访问隐式捕获的变量。

以下示例包含通过值显式捕获变量 n 并通过引用隐式捕获变量 m 的 lambda 表达式:

// captures_lambda_expression.cpp
// compile with: /W4 /EHsc
#include <iostream>
using namespace std;

int main()
{
   int m = 0;
   int n = 0;
   [&, n] (int a) mutable { m = ++n + a; }(4);
   cout << m << endl << n << endl;
}

输出:

  

由于变量 n 是通过值捕获的,因此在调用 lambda 表达式后,变量的值仍保持 0 不变。 mutable 规范允许在 lambda 中修改 n。

尽管 lambda 表达式只能捕获具有自动存储持续时间的变量,但你可以在 lambda 表达式的主体中使用具有静态存储持续时间的变量。 以下示例使用 generate 函数和 lambda 表达式为 vector 对象中的每个元素赋值。 lambda 表达式将修改静态变量以生成下一个元素的值。

void fillVector(vector<int>& v)
{
    // A local static variable.
    static int nextValue = 1;

    // The lambda expression that appears in the following call to
    // the generate function modifies and uses the local static
    // variable nextValue.
    generate(v.begin(), v.end(), [] { return nextValue++; });
    //WARNING: this is not thread-safe and is shown for illustration only
}

有关详细信息,请参阅生成

下面的代码示例使用上一示例中的函数,并添加了使用 STL 算法 generate_n 的 lambda 表达式的示例。 该 lambda 表达式将 vector 对象的元素指派给前两个元素之和。 使用了 mutable 关键字,以使 lambda 表达式的主体可以修改 lambda 表达式通过值捕获的外部变量 x 和 y 的副本。 由于 lambda 表达式通过值捕获原始变量 x 和 y,因此它们的值在 lambda 执行后仍为 1。

// compile with: /W4 /EHsc
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c) {
    cout << s;

    for (const auto& e : c) {
        cout << e << " ";
    }

    cout << endl;
}

void fillVector(vector<int>& v)
{
    // A local static variable.
    static int nextValue = 1;

    // The lambda expression that appears in the following call to
    // the generate function modifies and uses the local static
    // variable nextValue.
    generate(v.begin(), v.end(), [] { return nextValue++; });
    //WARNING: this is not thread-safe and is shown for illustration only
}

int main()
{
    // The number of elements in the vector.
    const int elementCount = 9;

    // 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;

    // Sets each element in the vector to the sum of the
    // previous two elements.
    generate_n(v.begin() + 2,
        elementCount - 2,
        [=]() mutable throw() -> int { // lambda is the 3rd parameter
        // Generate current value.
        int n = x + y;
        // Update previous two values.
        x = y;
        y = n;
        return n;
    });
    print("vector v after call to generate_n() with lambda: ", v);

    // 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: " << x << " y: " << y << endl;

    // Fill the vector with a sequence of numbers
    fillVector(v);
    print("vector v after 1st call to fillVector(): ", v);
    // Fill the vector with the next sequence of numbers
    fillVector(v);
    print("vector v after 2nd call to fillVector(): ", v);
}

输出:

  

有关详细信息,请参阅 generate_n

Microsoft 专用的修饰符

若使用 Microsoft 专用的修饰符(例如 __declspec),则你可以紧接在 parameter-declaration-clause 后将其插入到 lambda 表达式,例如:

auto Sqr = [](int t) __declspec(code_seg("PagedMem")) -> int { return t*t; };

若要确定 lambda 是否支持某个修饰符,请参阅文档的 Microsoft 专用的修饰符部分中有关此内容的文章。

请参见

参考

C++ 中的 Lambda 表达式

Lambda 表达式的示例

生成

generate_n

for_each

异常规范

编译器警告(等级 1)C4297

Microsoft 专用的修饰符