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参数时,都会像代码1的实现一样传入一个实现了该Concept的类型,例如go_de就实现了unique_ptr 的模板参数Deletor

今天想尝试一下使用lambda表达式的类型作为模板参数传入,发现不行。原因在于

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 表达式没有默认的构造函数,operator=也被置为deleted。只有一个默认的复制构造函数和move构造函数。很显然,unique_ptr 的实现肯定是用到了Deletor Concept的默认构造函数的。所以编译不通过。这个在

unique_ptr构造函数页写的很清楚。

  1. 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.

设想unique_ptr( pointer p, d1 );构造函数不存在,那Lambda类型就没法作为Concept传入了。


总结

  • 想用Lambda表达式的类型作为Concept,使用类型推导关键字decltype
  • Lambda的类型没有default constructor、copy assignment operator.
  • 写C++库的时候,如果用到模板和Concept技术,要考虑添加Concept对象做参数的类型的构造函数从而才能不限制Lambda表达式类型作为Concept传入。

    毕竟,C++语言设计的原则是尽量不限制C++语言的用户的编程方式

C++ 用lambda代替 unique_ptr 的Deleter的更多相关文章

  1. C++:为什么unique_ptr的Deleter是模板类型参数,而shared_ptr的Deleter不是?

    为什么unique_ptr的Deleter是模板类型参数,而shared_ptr的Deleter不是? template <class T, class D = default_delete&l ...

  2. std::unique_ptr使用incomplete type的报错分析和解决

    Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...

  3. 智能指针之 unique_ptr

    对于动态申请的内存,C++语言为我们提供了new和delete运算符, 而没有像java一样,提供一个完整的GC机制,因此对于我们申请的动态内存, 我们需要时刻记得释放,且不能重复释放,释放后不能再去 ...

  4. C++智能指针 unique_ptr

    C++智能指针 unique_ptr unique_ptr 独占所指向的对象, 同一时刻只能有一个 unique_ptr 指向给定对象(通过禁止拷贝语义, 只有移动语义来实现), 定义于 memory ...

  5. 怎样在C++中获得完整的类型名称

    Wrote by mutouyun. (http://darkc.at/cxx-get-the-name-of-the-given-type/) 地球人都知道C++里有一个typeid操作符能够用来获 ...

  6. C++ Standard Library

    C++ Standard Library *注:内容主要是对參考1的学习记录.知识点与图片大都来源于该书, 部分知识点与图片来源于參考2. 详细參考信息,见最下方參考. * C++98中新支持的语言特 ...

  7. UVW源码漫谈(三)

    咱们继续看uvw的源码,这次看的东西比较多,去除底层的一些东西,很多代码都是连贯的,耦合度也比较高了.主要包括下面几个文件的代码: underlying_type.hpp resource.hpp l ...

  8. 《C++ Primer》学习总结;兼论如何使用'书'这种帮助性资料

    6.25~ 6.27,用了3天翻了一遍<C++ Primer>. ▶书的 固有坏处 一句话: 代码比 文字描述 好看多了.————> 直接看习题部分/ 看demo就行了 看文字在描述 ...

  9. C++11智能指针原理和实现

    一.智能指针起因 在C++中,动态内存的管理是由程序员自己申请和释放的,用一对运算符完成:new和delete. new:在动态内存中为对象分配一块空间并返回一个指向该对象的指针: delete:指向 ...

随机推荐

  1. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  2. 创建PDF模板,java添加内容、导出下载PDF

    本文主要内容是:用java在pdf模板中加入数据,图片. 废话不多说,举个非常简单的例子: 首先创建word文档,导出PDF. 用 软件adobe acrobat打开,操作步骤如图: 在指定位置添加文 ...

  3. C++基本内置类型

    C++基本内置类型 基本内置类型包括算术类型和空类型. 算术类型 算术类型包括整型和浮点型. 类型 含义 最小尺寸 bool 布尔型 - char 字符型 8 bit wchar_t 宽字符型 16 ...

  4. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

  5. FarmCraft[POI2014]

    题目描述 In a village called Byteville, there are   houses connected with N-1 roads. For each pair of ho ...

  6. hdu--1028--Ignatius and the Princess III (母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  7. How to create a jump server in AWS VPC

    本来是写的Word文档,给其他国家的同时看的,所以一开始就是英文写的,也没打算翻译成为中文了,顺便抱怨下,网上资料找了很久的资料都没有看到介绍怎么在单机环境下搭建RD Gateway的,写本文的目的是 ...

  8. js判断移动终端(手机浏览器)

    方法1: <script type="text/javascript">         var browser = {             versions: f ...

  9. 66. Plus One【leetcode】

    Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...

  10. [算法题] 3Sum Closest

    题目内容 Given an array S of n integers, find three integers in S such that the sum is closest to a give ...