結合C++11新特性來學習C++中lambda運算式的用法

來源:互聯網
上載者:User
在 C++ 11 中,lambda 運算式(通常稱為 "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 的組成部分:

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 子句中引入並初始化新的變數,而無需使這些變數存在於 匿名函式的封閉範圍內。 初始化可以任何任意運算式表示;且將從該運算式產生的類型推導新變數的類型。 此功能的一個好處是,在 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 /EHscint 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 intauto 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;}

輸出:

  50


由於變數 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 34x: 1 y: 1vector v after 1st call to fillVector(): 1 2 3 4 5 6 7 8 9vector v after 2nd call to fillVector(): 10 11 12 13 14 15 16 17 18
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.