C++ 用lambda代替 unique_ptr 的Deleter
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
构造函数页写的很清楚。
- 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的更多相关文章
- C++:为什么unique_ptr的Deleter是模板类型参数,而shared_ptr的Deleter不是?
为什么unique_ptr的Deleter是模板类型参数,而shared_ptr的Deleter不是? template <class T, class D = default_delete&l ...
- std::unique_ptr使用incomplete type的报错分析和解决
Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...
- 智能指针之 unique_ptr
对于动态申请的内存,C++语言为我们提供了new和delete运算符, 而没有像java一样,提供一个完整的GC机制,因此对于我们申请的动态内存, 我们需要时刻记得释放,且不能重复释放,释放后不能再去 ...
- C++智能指针 unique_ptr
C++智能指针 unique_ptr unique_ptr 独占所指向的对象, 同一时刻只能有一个 unique_ptr 指向给定对象(通过禁止拷贝语义, 只有移动语义来实现), 定义于 memory ...
- 怎样在C++中获得完整的类型名称
Wrote by mutouyun. (http://darkc.at/cxx-get-the-name-of-the-given-type/) 地球人都知道C++里有一个typeid操作符能够用来获 ...
- C++ Standard Library
C++ Standard Library *注:内容主要是对參考1的学习记录.知识点与图片大都来源于该书, 部分知识点与图片来源于參考2. 详细參考信息,见最下方參考. * C++98中新支持的语言特 ...
- UVW源码漫谈(三)
咱们继续看uvw的源码,这次看的东西比较多,去除底层的一些东西,很多代码都是连贯的,耦合度也比较高了.主要包括下面几个文件的代码: underlying_type.hpp resource.hpp l ...
- 《C++ Primer》学习总结;兼论如何使用'书'这种帮助性资料
6.25~ 6.27,用了3天翻了一遍<C++ Primer>. ▶书的 固有坏处 一句话: 代码比 文字描述 好看多了.————> 直接看习题部分/ 看demo就行了 看文字在描述 ...
- C++11智能指针原理和实现
一.智能指针起因 在C++中,动态内存的管理是由程序员自己申请和释放的,用一对运算符完成:new和delete. new:在动态内存中为对象分配一块空间并返回一个指向该对象的指针: delete:指向 ...
随机推荐
- redis 订阅与发布
PUBLISH,SUBSCRIBE,等命令实现订阅与发布 订阅/发布到频道 订阅/发布到模式 频道的订阅与信息发送 订阅subscribe,可以让客户端订阅任意数量的频道, 每当有新信息发送到 ...
- 用Python识别网站使用的技术
在进行爬虫之前,一般我们都会对要爬取的网站进行识别,识别我们要爬取的网站所使用到的技术,这样才能更有利于我们爬虫工作的进行.所以在此介绍以下如何用Python去识别一个网站所使用到的技术. 环境:Py ...
- year:2017 month:8 day:1+
2017-08-01 JAVAse 方法的重载:在同一个类中存在一个以上的同名方法,只要他们的参数数量,参数类型,参数顺序(两个相同类型的参数是不行的)这样就构成了方法的重载. 有返回值的方法有三种调 ...
- 基于angular2x+ng-bootstrap构建后台管理系统界面(干货)
写在前面的话 近来公司要做一个后台管理系统,人手比较少,于是作为一个前端也参与进来,其实据我所知,大部分的公司还是后台自己捣鼓的. 在后台没有到位的情况下,前端应该使用什么技术也着实让我为难了一把.经 ...
- 双向lstm-crf源码的问题和细微修改
别人的源码地址:https://github.com/chilynn/sequence-labeling/ 如果你训练就会发现loss降到0以下,按照他设定的目标函数,loss理论上应该是大于0的,仔 ...
- 9. leetcode 389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- 5. Leetcode 448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- HDU 6043 KazaQ's Socks (规律)
Description KazaQ wears socks everyday. At the beginning, he has nn pairs of socks numbered from 11 ...
- vscode--搭建自动编译sass环境
一,安装插件及使用步骤 1.vscode安装Live Sass Compiler,由于该插件依赖Live Server ,所以会自动安装Live Server 2.点击vscode底部的Watch m ...
- windows本地提权对照表(转载)
2003 systeminfo>C:\Windows\Temp\temp.txt&(for %i in (KB3057191 KB2840221 KB3000061 KB2850 ...