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類型就沒法作為Concept傳入了。
總結(jié)
- 想用Lambda表達(dá)式的類型作為Concept,使用類型推導(dǎo)關(guān)鍵字decltype
 - Lambda的類型沒有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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
上一篇:C++調(diào)用Python基礎(chǔ)功能實(shí)例詳解
欄 目:C語(yǔ)言
下一篇:數(shù)據(jù)結(jié)構(gòu)與算法中二叉樹子結(jié)構(gòu)的詳解
本文標(biāo)題:C++ 中使用lambda代替 unique_ptr 的Deleter的方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1628.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
 - 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
 - 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
 - 01-10深入理解C++中常見的關(guān)鍵字含義
 - 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
 - 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
 - 01-10APUE筆記之:進(jìn)程環(huán)境詳解
 - 01-10c++中inline的用法分析
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
 - 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
 - 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-10delphi制作wav文件的方法
 - 04-02jquery與jsp,用jquery
 - 01-10C#中split用法實(shí)例總結(jié)
 


