這篇文章主要介紹C++中如何使用lambda代替unique_ptr的Deleter,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司長期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為遂川企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、做網(wǎng)站,遂川網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
代碼
#include <iostream> #include <cstdlib> #include <memory> #include <string> #include <functional> using namespace std; class go { public: go() {} ~go() { cout << "go die.\n"; } }; auto d = [] ( go * gp ) { delete gp; cout << "deletor done.\n"; }; class go_de { public: void operator() ( go* g ) { d ( g ); } }; int main() { { unique_ptr < go, go_de > b{ new go{} };//1 } { //unique_ptr < go, decltype (d) > b{ new go{}}; complie error //2 unique_ptr < go, decltype (d) > a{ new go{}, d };//3 } { unique_ptr < go, function<void(go*) > > a{ new go{}, d };//4 //i.e. unique_ptr < go, function<void(go*) > > a{ new go{}, [](go*gp) {delete gp;cout << "deletor done.\n"; }}; } system ( "pause" ); return 0; }
描述
一般的,需要給一個(gè)模板的Concept參數(shù)時(shí),都會(huì)像代碼1的實(shí)現(xiàn)一樣傳入一個(gè)實(shí)現(xiàn)了該Concept的類型,例如go_de就實(shí)現(xiàn)了unique_ptr 的模板參數(shù)Deletor。
今天想嘗試一下使用lambda表達(dá)式的類型作為模板參數(shù)傳入,發(fā)現(xiàn)不行。原因在于
c++14 draft n4269
5.1.2 Lambda expressions
20 The closure type associated with a lambda-expression has no default constructor and a deleted copy assignment operator. It has a defaulted copy constructor and a defaulted move constructor (12.8). [ Note: These special member functions are implicitly defined as usual, and might therefore be defined as deleted. end note ]
意思就是 lambda 表達(dá)式?jīng)]有默認(rèn)的構(gòu)造函數(shù),operator=也被置為deleted。只有一個(gè)默認(rèn)的復(fù)制構(gòu)造函數(shù)和move構(gòu)造函數(shù)。很顯然,unique_ptr 的實(shí)現(xiàn)肯定是用到了Deletor Concept的默認(rèn)構(gòu)造函數(shù)的。所以編譯不通過。這個(gè)在
unique_ptr構(gòu)造函數(shù)頁寫的很清楚。
2) Constructs a std::unique_ptr which owns p, initializing the stored pointer with p and value-initializing the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.2) Constructs a std::unique_ptr which owns p, initializing the stored pointer with p and value-initializing the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.
設(shè)想unique_ptr( pointer p, d1 );構(gòu)造函數(shù)不存在,那Lambda類型就沒法作為Concept傳入了。
總結(jié)
想用Lambda表達(dá)式的類型作為Concept,使用類型推導(dǎo)關(guān)鍵字decltype
Lambda的類型沒有default constructor、copy assignment operator.
寫C++庫的時(shí)候,如果用到模板和Concept技術(shù),要考慮添加Concept對(duì)象做參數(shù)的類型的構(gòu)造函數(shù)從而才能不限制Lambda表達(dá)式類型作為Concept傳入。
畢竟,C++語言設(shè)計(jì)的原則是盡量不限制C++語言的用戶的編程方式。
以上是“C++中如何使用lambda代替unique_ptr的Deleter”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標(biāo)題名稱:C++中如何使用lambda代替unique_ptr的Deleter
鏈接地址:http://www.rwnh.cn/article36/pgcosg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、Google、動(dòng)態(tài)網(wǎng)站、ChatGPT、標(biāo)簽優(yōu)化、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)