C++ 中使用lambda代替 unique_ptr 的Deleter的方法
代碼
#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; }
描述
一般的,需要給一個模板的Concept參數(shù)時,都會像代碼1的實現(xiàn)一樣傳入一個實現(xiàn)了該Concept的類型,例如go_de就實現(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。只有一個默認(rèn)的復(fù)制構(gòu)造函數(shù)和move構(gòu)造函數(shù)。很顯然,unique_ptr 的實現(xiàn)肯定是用到了Deletor Concept的默認(rèn)構(gòu)造函數(shù)的。所以編譯不通過。這個在
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++庫的時候,如果用到模板和Concept技術(shù),要考慮添加Concept對象做參數(shù)的類型的構(gòu)造函數(shù)從而才能不限制Lambda表達(dá)式類型作為Concept傳入。
畢竟,C++語言設(shè)計的原則是盡量不限制C++語言的用戶的編程方式。
以上所述是小編給大家介紹的C++ 中使用lambda代替 unique_ptr 的Deleter的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
關(guān)于C++11的統(tǒng)一初始化語法示例詳解
C++之前的初始化語法很亂,有四種初始化方式,而且每種之前甚至不能相互轉(zhuǎn)換,但從C++11出現(xiàn)后就好了,所以這篇文章主要給大家介紹了關(guān)于C++11的統(tǒng)一初始化語法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-10-10C++算術(shù)運(yùn)算符與類型轉(zhuǎn)換
這篇文章主要介紹了C++算術(shù)運(yùn)算符與類型轉(zhuǎn)換,C++當(dāng)中提供5種基礎(chǔ)的算術(shù)運(yùn)算符,分別是加法、減法、乘法、除法和取模。下main我們就一起來看看下面文章得具體舉例與說明,需要的朋友可以參考一下,希望對你有所幫助2021-11-11OpenCV圖像特征提取之Shi-Tomasi角點(diǎn)檢測算法詳解
Harris角點(diǎn)檢測算法就是對角點(diǎn)響應(yīng)函數(shù)R進(jìn)行閾值處理,Shi-Tomasi原理幾乎和Harris一樣的,只不過最后計算角點(diǎn)響應(yīng)的公式發(fā)生了變化。本文將和大家詳細(xì)說說Shi-Tomasi角點(diǎn)檢測算法的原理與實現(xiàn),需要的可以參考一下2022-09-09C++實現(xiàn)LeetCode(24.成對交換節(jié)點(diǎn))
這篇文章主要介紹了C++實現(xiàn)LeetCode(24.成對交換節(jié)點(diǎn)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07詳解C語言用malloc函數(shù)申請二維動態(tài)數(shù)組的實例
這篇文章主要介紹了詳解C語言用malloc函數(shù)申請二維動態(tài)數(shù)組的實例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10c語言中位字段與結(jié)構(gòu)聯(lián)合的組合使用詳解
本篇文章是對c語言中位字段與結(jié)構(gòu)聯(lián)合的組合使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05