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;
}
描述
一般的,需要給一個(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ù)的。所以編譯不通過(guò)。這個(gè)在
unique_ptr構(gòu)造函數(shù)頁(yè)寫的很清楚。
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類型就沒(méi)法作為Concept傳入了。
總結(jié)
- 想用Lambda表達(dá)式的類型作為Concept,使用類型推導(dǎo)關(guān)鍵字decltype
- Lambda的類型沒(méi)有default constructor、copy assignment operator.
- 寫C++庫(kù)的時(shí)候,如果用到模板和Concept技術(shù),要考慮添加Concept對(duì)象做參數(shù)的類型的構(gòu)造函數(shù)從而才能不限制Lambda表達(dá)式類型作為Concept傳入。
畢竟,C++語(yǔ)言設(shè)計(jì)的原則是盡量不限制C++語(yǔ)言的用戶的編程方式。
以上所述是小編給大家介紹的C++ 中使用lambda代替 unique_ptr 的Deleter的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
linux下C/C++學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了linux下c/c++學(xué)生信息管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C語(yǔ)言約瑟夫環(huán)的實(shí)現(xiàn)
這篇文章主要介紹了C語(yǔ)言約瑟夫環(huán)的實(shí)現(xiàn)的相關(guān)資料,這里主要是利用數(shù)據(jù)數(shù)據(jù)結(jié)果中循環(huán)鏈表來(lái)實(shí)現(xiàn),需要的朋友可以參考下2017-08-08
C++類靜態(tài)成員與類靜態(tài)成員函數(shù)詳解
靜態(tài)成員不可在類體內(nèi)進(jìn)行賦值,因?yàn)樗潜凰性擃惖膶?duì)象所共享的。你在一個(gè)對(duì)象里給它賦值,其他對(duì)象里的該成員也會(huì)發(fā)生變化。為了避免混亂,所以不可在類體內(nèi)進(jìn)行賦值2013-09-09
C語(yǔ)言將數(shù)組中元素的數(shù)排序輸出的相關(guān)問(wèn)題解決
這篇文章主要介紹了C語(yǔ)言將數(shù)組中元素的數(shù)排序輸出的相關(guān)問(wèn)題解決,文中的題目是將元素連接起來(lái)排成一個(gè)數(shù)并要求出這類結(jié)果中數(shù)最小的一個(gè),需要的朋友可以參考下2016-03-03
C++?Boost?weak_ptr智能指針超詳細(xì)講解
智能指針是一種像指針的C++對(duì)象,但它能夠在對(duì)象不使用的時(shí)候自己銷毀掉。雖然STL提供了auto_ptr,但是由于不能同容器一起使用(不支持拷貝和賦值操作),因此很少有人使用。它是Boost各組件中,應(yīng)用最為廣泛的一個(gè)2022-11-11
Qt數(shù)據(jù)庫(kù)相關(guān)應(yīng)用開(kāi)發(fā)總結(jié)
這篇文章主要為大家介紹了在Qt數(shù)據(jù)庫(kù)應(yīng)用開(kāi)發(fā)中的一些經(jīng)驗(yàn)總結(jié),以及一些組件的使用介紹。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02
c語(yǔ)言輸出字符串中最大對(duì)稱子串長(zhǎng)度的3種解決方案
這篇文章主要介紹了c語(yǔ)言輸出字符串中最大對(duì)稱子串長(zhǎng)度的3種解決方案,需要的朋友可以參考下2014-03-03

