C++ 中的 Lambda 表达式

原网址:C++ 中的 Lambda 表达式 | Microsoft Docs

在 C++11 及更高版本中,lambda 表达式(通常称为 lambda)是定义匿名函数对象的便捷方法, (关闭) 直接在调用或作为参数传递给函数的位置。 通常,lambda 用于封装传递到算法或异步函数的几行代码。 本文定义 lambda 是什么,并将其与其他编程技术进行比较。 它描述了它们的优点,并提供一些基本示例。

lambda 表达式的各个部分

ISO C++ 标准展示了作为第三个参数传递给 std::sort() 函数的简单 lambda:

#include <algorithm>
#include <cmath>

void abssort(float* x, unsigned n) {
    std::sort(x, x + n,
        // Lambda expression begins
        [](float a, float b) {
            return (std::abs(a) < std::abs(b));
        } // end of lambda expression
    );
}

此图显示了 lambda 的组成部分:

  1. capture 子句 (C++ 规范中也称为 lambda 引入器 。)

  2. 参数列表 选。 (也称为 lambda 声明符)

  3. 可变规范 选。

  4. exception-specification 选。

  5. trailing-return-type 选。

  6. lambda 正文

Capture 子句

lambda 可以在 C++14) 的主体 (中引入新变量,还可以从周围范围访问或 捕获变量。 lambda 以捕获子句开头。 它指定捕获哪些变量,以及捕获是按值还是按引用捕获。 具有 ampersand () & 前缀的变量通过引用访问,而没有前缀的变量则通过值访问。

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

可以使用捕获默认模式来指示如何捕获 lambda 正文中引用的任何外部变量: [&] 表示引用的所有变量都是通过引用捕获的,并且 [=] 意味着它们按值捕获。 可以使用默认捕获模式,然后为特定变量显式指定相反的模式。 例如,如果 lambda 体通过引用访问外部变量 total 并通过值访问外部变量 factor,则以下 capture 子句等效:

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

使用捕获默认值时,仅捕获 lambda 正文中提到的变量。

如果捕获子句包含捕获默认值 &,则捕获该捕获子句中的标识符不能具有该格式 &identifier。 同样,如果捕获子句包含捕获默认值 =,则无法捕获该捕获子句具有该窗体 =identifier。 标识符或 this 无法在捕获子句中多次出现。 以下代码片段演示了一些示例:C++复制

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
    [=, *this]{ }; // OK: captures this by value. See below.
    [i, i]{};      // ERROR: i repeated
}

后跟省略号的捕获是包扩展,如此 可变模板 示例所示:

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

若要在类成员函数的正文中使用 lambda 表达式,请 this 传递指向捕获子句的指针,以提供对封闭类的成员函数和数据成员的访问权限。

Visual Studio 2017 版本 15.3 及更高版本 (在/std:c++17模式及更高版本中可用) :this可以通过在捕获子句中指定*this值来捕获指针。 按值捕获会将整个关闭复制到调用 lambda 的每个调用站点。 (关闭是封装 lambda 表达式的匿名函数对象。) 当 lambda 并行或异步操作执行时,按值捕获非常有用。 它在某些硬件体系结构(如 NUMA)上特别有用。

有关演示如何对类成员函数使用 lambda 表达式的示例,请参阅 lambda 表达式示例中的“示例:在方法中使用 lambda 表达式”。

使用捕获子句时,我们建议记住以下几点,尤其是在将 lambda 与多线程配合使用时:

  • 引用捕获可用于修改外部变量,但值捕获不能。 mutable (允许修改副本,但不允许修改 originals.)

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

  • 引用捕获引入生存期依赖项,而值捕获却没有生存期依赖项。 当 lambda 异步运行时,这一点尤为重要。 如果在异步 lambda 中按引用捕获本地,则运行 lambda 时,很容易会消失该本地。 代码可能会导致运行时发生访问冲突。

通用捕获 (C++14)

在 C++14 中,可在 Capture 子句中引入并初始化新的变量,而无需使这些变量存在于 lambda 函数的封闭范围内。 初始化可以任何任意表达式表示;且将从该表达式生成的类型推导新变量的类型。 此功能允许捕获仅移动变量 (,例如 std::unique_ptr 从周围范围) 并在 lambda 中使用它们。

pNums = make_unique<vector<int>>(nums);
//...
      auto a = [ptr = move(pNums)]()
        {
           // use ptr
        };

参数列表

Lambda 既可以捕获变量,也可以接受输入参数。 标准语法 () 中的 lambda 声明符 的参数列表是可选的,大多数方面都类似于函数的参数列表。

auto y = [] (int first, int second)
{
    return first + second;
};

在 C++14 中,如果参数类型为泛型,则可以使用 auto 关键字作为类型说明符。 此关键字告知编译器创建函数调用运算符作为模板。 参数列表中的每个实例 auto 都等效于不同的类型参数。

C++复制

auto y = [] (auto first, auto second)
{
    return first + second;
};

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

由于参数列表是可选的,如果不将参数传递给 lambda 表达式,并且其 lambda 声明器不包含 异常规范、 尾随-return-type 或 mutable,则可以省略空括号。

可变规范

通常,lambda 的函数调用运算符是常量值,但使用 mutable 关键字会取消此操作。它不生成可变数据成员。 该 mutable 规范使 lambda 表达式的正文能够修改值捕获的变量。 本文后面的一些示例演示如何使用 mutable

异常规范

可以使用 noexcept 异常规范来指示 lambda 表达式不会引发任何异常。 与普通函数一样,如果 lambda 表达式声明noexcept异常规范并且 lambda 正文引发异常,则 Microsoft C++ 编译器会生成警告 C4297,如下所示:

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

有关详细信息,请参阅 异常规范 (引发) 

返回类型

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

如果 lambda 正文仅包含一个 return 语句,则可以省略 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 isn't valid

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

Lambda 正文

lambda 表达式的 lambda 主体是复合语句。 它可以包含普通函数或成员函数正文中允许的任何内容。 普通函数和 lambda 表达式的主体均可访问以下变量类型:

  • 从封闭范围捕获变量,如前所述。

  • 参数。

  • 本地声明的变量。

  • 类数据成员在类内声明并 this 捕获时。

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

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

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

5
0

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

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 isn't thread-safe and is shown for illustration only
}

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

下面的代码示例使用上一示例中的函数,并添加使用 C++ 标准库算法 generate_n的 lambda 表达式的示例。 该 lambda 表达式将 vector 对象的元素指派给前两个元素之和。 mutable使用该关键字,以便 lambda 表达式的正文可以修改其外部变量x的副本,lambda 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 isn't 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);
}

vector v after call to generate_n() with lambda: 1 1 2 3 5 8 13 21 34
x: 1 y: 1
vector v after 1st call to fillVector(): 1 2 3 4 5 6 7 8 9
vector v after 2nd call to fillVector(): 10 11 12 13 14 15 16 17 18

有关详细信息,请参阅 generate_n

constexpr lambda 表达式

Visual Studio 2017 版本 15.3 及更高版本 (在/std:c++17模式和更高版本中可用) :在常量) 表达式中允许初始化每个捕获或引入的数据成员时,可以将 lambda 表达式声明为constexpr (或使用它。

    int y = 32;
    auto answer = [y]() constexpr
    {
        int x = 10;
        return y + x;
    };

    constexpr int Increment(int n)
    {
        return [n] { return n + 1; }();
    }

如果 lambda 结果满足函数的要求,则 lambda 是 constexpr 隐式的 constexpr :

    auto answer = [](int n)
    {
        return 32 + n;
    };

    constexpr int response = answer(10);

如果 lambda 是隐式或显式 constexpr的,则转换为函数指针将生成一个 constexpr 函数:

    auto Increment = [](int n)
    {
        return n + 1;
    };

    constexpr int(*inc)(int) = Increment;

Microsoft 专用

以下公共语言运行时不支持 Lambda (CLR) 托管实体:ref classref structvalue classvalue struct

如果使用 Microsoft 特定的修饰符(例如 __declspec),则可以在之后立即 parameter-declaration-clause将其插入 lambda 表达式中。 例如:

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

若要确定 lambda 是否支持特定修饰符,请参阅 有关 Microsoft 特定修饰 符部分中的修饰符的文章。

Visual Studio支持 C++11 标准 lambda 功能和无状态 lambda。 无状态 lambda 可转换为使用任意调用约定的函数指针。

请参阅

C++ 语言参考
C++ 标准库中的函数对象
函数调用
for_each

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值