論C++的lambda是函數(shù)還是對(duì)象
先說結(jié)論:
- 對(duì)于有捕獲的
lambda,其等價(jià)于對(duì)象。 - 對(duì)于沒有任何捕獲的
lambda,其等價(jià)于函數(shù)!
首先,很多C++程序員從lambda 用法上反推容易發(fā)現(xiàn)是對(duì)象,因?yàn)閘ambda可以捕獲!這是函數(shù)做不到的。的確,比如:
int n = 100;
auto foo = [n](int a) {
? ? return a > n;
};
cout<< foo(99);如果編譯器要實(shí)現(xiàn)foo,大致類比這種寫法(可能真實(shí)的實(shí)現(xiàn)細(xì)節(jié)不是這樣,但思路類似)∶
struct Foo {
? ? Foo(int i) {n=i;}
? ? bool operator()(int a) {
? ? ? ? return a > n;
? ? }
private:
? ? int n;
};
...
int n = 100;
Foo foo(n);
cout<< foo(99);如果是引用捕獲了變量,那么struct內(nèi)有一個(gè)指針成員持有被引用捕獲的變量的地址。
比如:
set<int> ns = {100, 200, 300};
auto foo = [&ns](int a) {
? ? return ns.find(a);
};
cout<< foo(99);大致等價(jià)于:
struct Foo {
? ? Foo(set<int>* p) {p_ns = p;}
? ? bool operator()(int a) {
? ? ? ? auto &ns = *p-ns;
? ? ? ? return ns.find(a);
? ? }
private:
? ? set<int>* p_ns;
};
...
set<int> ns = {100, 200, 300};
Foo foo(&ns);
cout<< foo(99);然而……這并不是全部!
在沒有捕獲任何東西的時(shí)候,lambda其實(shí)是等價(jià)于普通的函數(shù)的!可以用Linux C中函數(shù)pthread_create()來驗(yàn)證!它只能接收一個(gè)參數(shù)是void*,返回值也是void*的回調(diào)函數(shù)。
神奇的是,無參的lambda也可以被pthread_create()使用!
using namespace std;
struct A {
? ? void* operator()(void*) {
? ? ? ? cout<<"xxxx"<<endl;
? ? ? ? return nullptr;
? ? }
};
int main() {
? ? A a;
? ? a(NULL);
? ? pthread_t t;
? ? //pthread_create(&t, NULL, a, NULL); // 編譯失敗
? ? auto cb = [](void*)->void* {
? ? ? ? cout<<"xxxx"<<endl;
? ? ? ? return nullptr;
? ? };
? ? pthread_create(&t, NULL, cb, NULL); // 編譯通過
? ? pthread_join(t, NULL);
? ? return 0;
}上面代碼還可以再改一下,讓cb去捕獲一個(gè)變量, 比如:
auto cb = [&](void*)->void* {
? ? ? ? cout<<"xxxx"<<endl;
? ? ? ? return nullptr;
? ? };
? ? pthread_create(&t, NULL, cb, NULL);這時(shí),給pthread_create()傳入cb同樣會(huì)編譯失?。?strong>錯(cuò)誤信息:
cb.cpp: In function ‘int main()': cb.cpp:23:30: error: cannot convert ‘main()::<lambda(void*)>' to ‘void* (*)(void*)' ? ?23 | ? ? pthread_create(&t, NULL, cb, NULL); ? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?^~ ? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?main()::<lambda(void*)> In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:35, ? ? ? ? ? ? ? ? ?from /usr/include/x86_64-linux-gnu/c++/9/bits/gthr.h:148, ? ? ? ? ? ? ? ? ?from /usr/include/c++/9/ext/atomicity.h:35, ? ? ? ? ? ? ? ? ?from /usr/include/c++/9/bits/ios_base.h:39, ? ? ? ? ? ? ? ? ?from /usr/include/c++/9/ios:42, ? ? ? ? ? ? ? ? ?from /usr/include/c++/9/ostream:38, ? ? ? ? ? ? ? ? ?from /usr/include/c++/9/iostream:39, ? ? ? ? ? ? ? ? ?from cb.cpp:1: /usr/include/pthread.h:200:15: note: ? initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)' ? 200 | ? ? ? void *(*__start_routine) (void *), ? ? ? | ? ? ? ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
這其實(shí)也不難理解,C++在lambda的設(shè)計(jì)上也貫徹著零開銷 (Zero Overhead)原則,也就是C++不在性能上干多余的事,顯然函數(shù)比對(duì)象開銷更小。所以即使同為lambda,在有無捕獲的時(shí)候,其底層實(shí)現(xiàn)其實(shí)是截然不同的!
到此這篇關(guān)于論C++的lambda是函數(shù)還是對(duì)象的文章就介紹到這了,更多相關(guān)C++中的lambda內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言修煉之路悟徹?cái)?shù)組真妙理?巧用下標(biāo)破萬敵上篇
在C語言和C++等語言中,數(shù)組元素全為指針變量的數(shù)組稱為指針數(shù)組,指針數(shù)組中的元素都必須具有相同的存儲(chǔ)類型、指向相同數(shù)據(jù)類型的指針變量。指針數(shù)組比較適合用來指向若干個(gè)字符串,使字符串處理更加方便、靈活2022-02-02
C語言實(shí)現(xiàn)注冊(cè)登錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)注冊(cè)登錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
C語言用棧和隊(duì)列實(shí)現(xiàn)的回文檢測(cè)功能示例
這篇文章主要介紹了C語言用棧和隊(duì)列實(shí)現(xiàn)的回文檢測(cè)功能,結(jié)合具體實(shí)例形式分析了C語言棧和隊(duì)列的定義及使用棧和隊(duì)列進(jìn)行回文檢測(cè)的操作技巧,需要的朋友可以參考下2017-06-06
C++?自增自減運(yùn)算符的實(shí)現(xiàn)示例
本文主要介紹了C++?自增自減運(yùn)算符的實(shí)現(xiàn)示例,自增和自減運(yùn)算符在C++中主要用于循環(huán)語句中,使循環(huán)變量的值自動(dòng)+1或者-1,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
關(guān)于函數(shù)調(diào)用方式__stdcall和__cdecl詳解
下面小編就為大家?guī)硪黄P(guān)于函數(shù)調(diào)用方式__stdcall和__cdecl詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09

