欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++ 中使用lambda代替 unique_ptr 的Deleter的方法

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

代碼

#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對象做參數(shù)的類型的構(gòu)造函數(shù)從而才能不限制Lambda表達(dá)式類型作為Concept傳入。

畢竟,C++語言設(shè)計(jì)的原則是盡量不限制C++語言的用戶的編程方式。

以上所述是小編給大家介紹的C++ 中使用lambda代替 unique_ptr 的Deleter的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!

上一篇:C++調(diào)用Python基礎(chǔ)功能實(shí)例詳解

欄    目:C語言

下一篇:數(shù)據(jù)結(jié)構(gòu)與算法中二叉樹子結(jié)構(gòu)的詳解

本文標(biāo)題:C++ 中使用lambda代替 unique_ptr 的Deleter的方法

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1628.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有