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 的组成部份:
Capture 子句(在 C++ 范例中也称为 lambda 指导。)
参数列表(可选)。 (也称为 lambda 声明符)
可变范例(可选)。
非常范例(可选)。
跟随返回范例(可选)。
“lambda 体”
Capture 子句
Lambda 可在其主体中引入新的变量(用 C++14),它还能够接见(或“捕捉”)周边局限内的变量。 Lambda 以 Capture 子句(范例语法中的 lambda 指导)开首,它指定要捕捉的变量以及是经由过程值照样援用举行捕捉。 有与号 (&) 前缀的变量经由过程援用接见,没有该前缀的变量经由过程值接见。
空 capture 子句 [ ] 指导 lambda 表达式的主体不接见关闭局限中的变量。
能够运用默许捕捉情势(范例语法中的 capture-default)来指导怎样捕捉 lambda 中援用的任何外部变量:[&] 示意经由过程援用捕捉援用的一切变量,而 [=] 示意经由过程值捕捉它们。 能够运用默许捕捉情势,然后为特定变量显式指定相反的情势。 比方,假如 lambda 体经由过程援用接见外部变量 total 并经由过程值接见外部变量 factor,则以下 capture 子句等效:
[&total, factor] [factor, &total] [&, factor] [factor, &] [=, &total] [&total, =]
运用 capture-default 时,只要 lambda 中说起的变量才会被捕捉。
假如 capture 子句包括 capture-default&,则该 capture 子句的 identifier 中没有任何 capture 可采纳 & 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 以异步体式格局运行时,这一点特别主要。 假如在异步 lambda 中经由过程援用捕捉当地变量,该当地变量将很可能在 lambda 运行时消逝,从而致使运行时接见争执。
通用捕捉 (C++14)
在 C++14 中,可在 Capture 子句中引入并初始化新的变量,而无需使这些变量存在于 lambda 函数的关闭局限内。 初始化能够任何恣意表达式示意;且将从该表达式生成的范例推导新变量的范例。 此功用的一个优点是,在 C++14 中,可从周边局限捕捉只挪动的变量(比方 std::unique_ptr)并在 lambda 中运用它们。
pNums = make_unique<vector<int>>(nums); //... auto a = [ptr = move(pNums)]() { // use ptr };
参数列表
除了捕捉变量,lambda 还可接收输入参数。 参数列表(在范例语法中称为 lambda 声明符)是可选的,它在大多数方面类似于函数的参数列表。
int y = [] (int first, int second) { return first + second; };
在 C++14 中,假如参数范例是泛型,则能够运用 auto 关键字作为范例说明符。 这将示知编译器将函数挪用运算符建立为模板。 参数列表中的每一个 auto 实例等效于一个差别的范例参数。
auto y = [] (auto first, auto second) { return first + second; };
lambda 表达式能够将另一个 lambda 表达式作为其参数。 有关详细信息,请参阅 Lambda 表达式的示例主题中的“高阶 Lambda 表达式”。
因为参数列表是可选的,因而在不将参数通报到 lambda 表达式,而且其 lambda-declarator: 不包括 exception-specification、trailing-return-type 或 mutable 的情况下,能够省略空括号。
可变范例
一般,lambda 的函数挪用运算符为 const-by-value,但对 mutable 关键字的运用可将其作废。 它不会生成可变的数据成员。 应用可变范例,lambda 表达式的主体能够修正经由过程值捕捉的变量。 本文背面的一些示例将显现怎样运用 mutable。
非常范例
你能够运用 throw() 非常范例来指导 lambda 表达式不会激发任何非常。 与一般函数一样,假如 lambda 表达式声明 C4297 非常范例且 lambda 体激发非常,Visual C++ 编译器将生成正告 throw(),以下所示:
// throw_lambda_expression.cpp // compile with: /W4 /EHsc int main() // C4297 expected { []() throw() { throw 5; }(); }
返回范例
将自动推导 lambda 表达式的返回范例。 无需运用 auto 关键字,除非指定跟随返回范例。 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 时)
具有静态存储持续时间的任何变量(比方,全局变量)
以下示例包括经由过程值显式捕捉变量 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; }
输出:
5 0
因为变量 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); }
输出:
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
更多连系C++11新特征来进修C++中lambda表达式的用法相干文章请关注ki4网!