結(jié)合C++11新特性來(lái)學(xué)習(xí)C++中l(wèi)ambda表達(dá)式的用法
在 C++ 11 中,lambda 表達(dá)式(通常稱(chēng)為 "lambda")是一種在被調(diào)用的位置或作為參數(shù)傳遞給函數(shù)的位置定義匿名函數(shù)對(duì)象的簡(jiǎn)便方法。 Lambda 通常用于封裝傳遞給算法或異步方法的少量代碼行。 本文定義了 lambda 是什么,將 lambda 與其他編程技術(shù)進(jìn)行比較,描述其優(yōu)點(diǎn),并提供一個(gè)基本示例。
Lambda 表達(dá)式的各部分
ISO C++ 標(biāo)準(zhǔn)展示了作為第三個(gè)參數(shù)傳遞給 std::sort() 函數(shù)的簡(jiǎn)單 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++ 規(guī)范中也稱(chēng)為 lambda 引導(dǎo)。)
- 參數(shù)列表(可選)。 (也稱(chēng)為 lambda 聲明符)
- 可變規(guī)范(可選)。
- 異常規(guī)范(可選)。
- 尾隨返回類(lèi)型(可選)。
- “l(fā)ambda 體”
Capture 子句
Lambda 可在其主體中引入新的變量(用 C++14),它還可以訪問(wèn)(或“捕獲”)周邊范圍內(nèi)的變量。 Lambda 以 Capture 子句(標(biāo)準(zhǔn)語(yǔ)法中的 lambda 引導(dǎo))開(kāi)頭,它指定要捕獲的變量以及是通過(guò)值還是引用進(jìn)行捕獲。 有與號(hào) (&) 前綴的變量通過(guò)引用訪問(wèn),沒(méi)有該前綴的變量通過(guò)值訪問(wèn)。
空 capture 子句 [ ] 指示 lambda 表達(dá)式的主體不訪問(wèn)封閉范圍中的變量。
可以使用默認(rèn)捕獲模式(標(biāo)準(zhǔn)語(yǔ)法中的 capture-default)來(lái)指示如何捕獲 lambda 中引用的任何外部變量:[&] 表示通過(guò)引用捕獲引用的所有變量,而 [=] 表示通過(guò)值捕獲它們。 可以使用默認(rèn)捕獲模式,然后為特定變量顯式指定相反的模式。 例如,如果 lambda 體通過(guò)引用訪問(wèn)外部變量 total 并通過(guò)值訪問(wèn)外部變量 factor,則以下 capture 子句等效:
[&total, factor] [factor, &total] [&, factor] [factor, &] [=, &total] [&total, =]
使用 capture-default 時(shí),只有 lambda 中提及的變量才會(huì)被捕獲。
如果 capture 子句包含 capture-default&,則該 capture 子句的 identifier 中沒(méi)有任何 capture 可采用 & identifier 形式。 同樣,如果 capture 子句包含 capture-default=,則該 capture 子句的 capture 不能采用 = identifier 形式。 identifier 或 this 在 capture 子句中出現(xiàn)的次數(shù)不能超過(guò)一次。 以下代碼片段給出了一些示例。
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 后跟省略號(hào)是包擴(kuò)展,如以下可變參數(shù)模板示例中所示:
template<class... Args> void f(Args... args) { auto x = [args...] { return g(args...); }; x(); }
要在類(lèi)方法的正文中使用 lambda 表達(dá)式,請(qǐng)將 this 指針傳遞給 Capture 子句,以提供對(duì)封閉類(lèi)的方法和數(shù)據(jù)成員的訪問(wèn)權(quán)限。 有關(guān)展示如何將 lambda 表達(dá)式與類(lèi)方法一起使用的示例,請(qǐng)參閱 Lambda 表達(dá)式的示例中的“示例:在方法中使用 Lambda 表達(dá)式”。
在使用 capture 子句時(shí),建議你記住以下幾點(diǎn)(尤其是使用采取多線程的 lambda 時(shí)):
引用捕獲可用于修改外部變量,而值捕獲卻不能實(shí)現(xiàn)此操作。 (mutable允許修改副本,而不能修改原始項(xiàng)。)
引用捕獲會(huì)反映外部變量的更新,而值捕獲卻不會(huì)反映。
引用捕獲引入生存期依賴(lài)項(xiàng),而值捕獲卻沒(méi)有生存期依賴(lài)項(xiàng)。 當(dāng) lambda 以異步方式運(yùn)行時(shí),這一點(diǎn)尤其重要。 如果在異步 lambda 中通過(guò)引用捕獲本地變量,該本地變量將很可能在 lambda 運(yùn)行時(shí)消失,從而導(dǎo)致運(yùn)行時(shí)訪問(wèn)沖突。
通用捕獲 (C++14)
在 C++14 中,可在 Capture 子句中引入并初始化新的變量,而無(wú)需使這些變量存在于 lambda 函數(shù)的封閉范圍內(nèi)。 初始化可以任何任意表達(dá)式表示;且將從該表達(dá)式生成的類(lèi)型推導(dǎo)新變量的類(lèi)型。 此功能的一個(gè)好處是,在 C++14 中,可從周邊范圍捕獲只移動(dòng)的變量(例如 std::unique_ptr)并在 lambda 中使用它們。
pNums = make_unique<vector<int>>(nums); //... auto a = [ptr = move(pNums)]() { // use ptr };
參數(shù)列表
除了捕獲變量,lambda 還可接受輸入?yún)?shù)。 參數(shù)列表(在標(biāo)準(zhǔn)語(yǔ)法中稱(chēng)為 lambda 聲明符)是可選的,它在大多數(shù)方面類(lèi)似于函數(shù)的參數(shù)列表。
int y = [] (int first, int second) { return first + second; };
在 C++14 中,如果參數(shù)類(lèi)型是泛型,則可以使用 auto 關(guān)鍵字作為類(lèi)型說(shuō)明符。 這將告知編譯器將函數(shù)調(diào)用運(yùn)算符創(chuàng)建為模板。 參數(shù)列表中的每個(gè) auto 實(shí)例等效于一個(gè)不同的類(lèi)型參數(shù)。
auto y = [] (auto first, auto second) { return first + second; };
lambda 表達(dá)式可以將另一個(gè) lambda 表達(dá)式作為其參數(shù)。 有關(guān)詳細(xì)信息,請(qǐng)參閱 Lambda 表達(dá)式的示例主題中的“高階 Lambda 表達(dá)式”。
由于參數(shù)列表是可選的,因此在不將參數(shù)傳遞到 lambda 表達(dá)式,并且其 lambda-declarator: 不包含 exception-specification、trailing-return-type 或 mutable 的情況下,可以省略空括號(hào)。
可變規(guī)范
通常,lambda 的函數(shù)調(diào)用運(yùn)算符為 const-by-value,但對(duì) mutable 關(guān)鍵字的使用可將其取消。 它不會(huì)生成可變的數(shù)據(jù)成員。 利用可變規(guī)范,lambda 表達(dá)式的主體可以修改通過(guò)值捕獲的變量。 本文后面的一些示例將顯示如何使用 mutable。
異常規(guī)范
你可以使用 throw() 異常規(guī)范來(lái)指示 lambda 表達(dá)式不會(huì)引發(fā)任何異常。 與普通函數(shù)一樣,如果 lambda 表達(dá)式聲明 C4297 異常規(guī)范且 lambda 體引發(fā)異常,Visual C++ 編譯器將生成警告 throw(),如下所示:
// throw_lambda_expression.cpp // compile with: /W4 /EHsc int main() // C4297 expected { []() throw() { throw 5; }(); }
返回類(lèi)型
將自動(dòng)推導(dǎo) lambda 表達(dá)式的返回類(lèi)型。 無(wú)需使用 auto 關(guān)鍵字,除非指定尾隨返回類(lèi)型。 trailing-return-type 類(lèi)似于普通方法或函數(shù)的返回類(lèi)型部分。 但是,返回類(lèi)型必須跟在參數(shù)列表的后面,你必須在返回類(lèi)型前面包含 trailing-return-type 關(guān)鍵字 ->。
如果 lambda 體僅包含一個(gè)返回語(yǔ)句或其表達(dá)式不返回值,則可以省略 lambda 表達(dá)式的返回類(lèi)型部分。 如果 lambda 體包含單個(gè)返回語(yǔ)句,編譯器將從返回表達(dá)式的類(lèi)型推導(dǎo)返回類(lèi)型。 否則,編譯器會(huì)將返回類(lèi)型推導(dǎo)為 void。 下面的代碼示例片段說(shuō)明了這一原則。
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 表達(dá)式可以生成另一個(gè) lambda 表達(dá)式作為其返回值。 有關(guān)詳細(xì)信息,請(qǐng)參閱 Lambda 表達(dá)式的示例中的“高階 Lambda 表達(dá)式”。
Lambda 體
lambda 表達(dá)式的 lambda 體(標(biāo)準(zhǔn)語(yǔ)法中的 compound-statement)可包含普通方法或函數(shù)的主體可包含的任何內(nèi)容。 普通函數(shù)和 lambda 表達(dá)式的主體均可訪問(wèn)以下變量類(lèi)型:
- 從封閉范圍捕獲變量,如前所述。
- 參數(shù)
- 本地聲明變量
- 類(lèi)數(shù)據(jù)成員(在類(lèi)內(nèi)部聲明并且捕獲 this 時(shí))
- 具有靜態(tài)存儲(chǔ)持續(xù)時(shí)間的任何變量(例如,全局變量)
以下示例包含通過(guò)值顯式捕獲變量 n 并通過(guò)引用隱式捕獲變量 m 的 lambda 表達(dá)式:
// 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 是通過(guò)值捕獲的,因此在調(diào)用 lambda 表達(dá)式后,變量的值仍保持 0 不變。 mutable 規(guī)范允許在 lambda 中修改 n。
盡管 lambda 表達(dá)式只能捕獲具有自動(dòng)存儲(chǔ)持續(xù)時(shí)間的變量,但你可以在 lambda 表達(dá)式的主體中使用具有靜態(tài)存儲(chǔ)持續(xù)時(shí)間的變量。 以下示例使用 generate 函數(shù)和 lambda 表達(dá)式為 vector 對(duì)象中的每個(gè)元素賦值。 lambda 表達(dá)式將修改靜態(tài)變量以生成下一個(gè)元素的值。
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 }
下面的代碼示例使用上一示例中的函數(shù),并添加了使用 STL 算法 generate_n 的 lambda 表達(dá)式的示例。 該 lambda 表達(dá)式將 vector 對(duì)象的元素指派給前兩個(gè)元素之和。 使用了 mutable 關(guān)鍵字,以使 lambda 表達(dá)式的主體可以修改 lambda 表達(dá)式通過(guò)值捕獲的外部變量 x 和 y 的副本。 由于 lambda 表達(dá)式通過(guò)值捕獲原始變量 x 和 y,因此它們的值在 lambda 執(zhí)行后仍為 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中l(wèi)ambda、std::function和std:bind詳解
- C++11/14 線程中使用Lambda函數(shù)的方法
- 淺談C++11新引入的lambda表達(dá)式
- 淺析C++11新特性的Lambda表達(dá)式
- 一文讀懂c++11 Lambda表達(dá)式
- C++11 lambda表達(dá)式在回調(diào)函數(shù)中的使用方式
- C++11?lambda(匿名函數(shù))表達(dá)式詳細(xì)介紹
- C++11中的可變參數(shù)模板/lambda表達(dá)式
- 深入解析C++11?lambda表達(dá)式/包裝器/線程庫(kù)
- 深入理解C++11:探索lambda函數(shù)的奧秘
相關(guān)文章
OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C語(yǔ)言實(shí)現(xiàn)賓館管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)賓館管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C語(yǔ)言利用system調(diào)用系統(tǒng)命令行詳情
這篇文章主要介紹了C語(yǔ)言利用system調(diào)用系統(tǒng)命令行詳情,system就是調(diào)用系統(tǒng)命令行,輸入為字符串,然后把這個(gè)字符串輸出給命令行,讓命令行執(zhí)行。下文的具體內(nèi)容,需要的小伙伴可以參考一下2022-01-01Visual?studio2022?利用glfw+glad配置OpenGL環(huán)境的詳細(xì)過(guò)程
這篇文章主要介紹了Visual?studio2022?利用glfw+glad配置OpenGL環(huán)境,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10詳解C語(yǔ)言結(jié)構(gòu)體中的char數(shù)組如何賦值
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言結(jié)構(gòu)體中的char數(shù)組如何賦值的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03C++實(shí)現(xiàn)簡(jiǎn)易的通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)易的通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06