unique_ptr使用lambda的问题

虚空大鸟 2015-12-20 03:43:08
刚学习到用智能指针,想问一下向unique_ptr传递删除器可以使用lambda表达式吗,如果可以,lambda表达式类型是什么呢?
...全文
275 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iyomumx 2016-01-02
  • 打赏
  • 举报
回复
lambda表达式是一个对象,转成函数指针是不可能的 不过可以用std::function
std::unique_ptr<int, std::function<void(int*)>> ptr(new int[5], [](int * p) { delete[] p; });
zhizhuode 2016-01-02
  • 打赏
  • 举报
回复
unique是支持lambda表达式比如 unique_ptr<int,void(*)(int*)>ptr(new int(1),[](int *p){delete p;}); 只是不能有捕获变量如果有捕获的变量的话要支持lambda 就要用的std::function讲上面例子中的函数指针换成function(void(int *))就可以了
fefe82 2015-12-21
  • 打赏
  • 举报
回复
引用 4 楼 wx199308 的回复:
[quote=引用 3楼我是你的主体 的回复:][quote=引用 2楼fefe82 的回复:]auto deleter = [](T* p){delete p;}; std::unique_ptr<T, decltype(deleter)> up(new T, deleter); 大概这样吧 ...
验证了,没问题,但和我想的有点不一样,请问能直接使用lambda表达式而不使用前面那一句auto表达式吗?[/quote]是不是因为lambda想要进行调用运算必须使用定义一个可调用变量这种方式。[/quote] 那句 auto 主要是为了后面 decltype 取出其类型。 每一个 lambda 表达式都有一个独立的类型。这个类型大概只能通过 decltype 拿到。 其实,另一种方案是用函数指针: std::unique_ptr<T, void(*)(T*)> p(new T, [](T*tp){delete tp;}); // 这个也没试过 ...
虚空大鸟 2015-12-20
  • 打赏
  • 举报
回复
引用 3楼我是你的主体 的回复:
[quote=引用 2楼fefe82 的回复:]auto deleter = [](T* p){delete p;}; std::unique_ptr<T, decltype(deleter)> up(new T, deleter); 大概这样吧 ...
验证了,没问题,但和我想的有点不一样,请问能直接使用lambda表达式而不使用前面那一句auto表达式吗?[/quote]是不是因为lambda想要进行调用运算必须使用定义一个可调用变量这种方式。
虚空大鸟 2015-12-20
  • 打赏
  • 举报
回复
引用 2楼fefe82 的回复:
auto deleter = [](T* p){delete p;}; std::unique_ptr<T, decltype(deleter)> up(new T, deleter); 大概这样吧 ...
验证了,没问题,但和我想的有点不一样,请问能直接使用lambda表达式而不使用前面那一句auto表达式吗?
fefe82 2015-12-20
  • 打赏
  • 举报
回复
auto deleter = [](T* p){delete p;}; std::unique_ptr<T, decltype(deleter)> up(new T, deleter); 大概这样吧 ...
虚空大鸟 2015-12-20
  • 打赏
  • 举报
回复
顶一下,求帮助
C++智能指针 ⼀ ⼀. 智能指针 智能指针 在 C++ 中,我们申请内存⼀般使⽤ new,释放内存时使⽤ delete,但是有时候我们可能由于疏忽会忘记 delete,或者当程序有多个 出⼝时,也会容易有 new 没有 delete,这样就产⽣了内存泄漏,如果你的程序是⼀个需要长期运⾏的服务器程序,可能运⾏着⼏天突然程 序就崩溃了,原因也不好定位,所以为了⽅便内存管理,C++ 引⼊了智能指针,智能指针的优点在于能够帮助程序员⾃动释放我们 new 出 来的堆内存。 C++ 标准库有四种智能指针:auto_ptrunique_ptr,shared_ptr,weak_ptr(auto_ptr 是 C++98 标准的,其余都是 C++11 标准推出的,auto_ptr 现在已经不再使⽤了),C++11 这三种智能指针都是类模板。 ⼆ ⼆. shared_ptr (⼀)概述 (⼀)概述 shared_ptr 是⼀个共享式指针,所有的 shared_ptr 共享对指向内存的所有权,不是被⼀个 shared_ptr 拥有,⽽是多个 shared_ptr 之间互相协作。 (⼆)⼯作原理 (⼆)⼯作原理 引⽤计数,use_count 为 0 时就释放对象空间。 (三)初始化 (三)初始化 如果是定义了⼀个智能指针却不初始化,shared_ptr p1,代表定义了⼀个指向 int 类型对象的智能指针但是⽬前指向为 empty。推荐使⽤ make_shared 函数来初始化 shared_ptr,它是标准库的函数模板,安全,⾼效地分配和使⽤ shared_ptr。 shared_ptr pint = make_shared(100); shared_ptr pstr = make_shared(5,'a'); 也可以使⽤直接初始化的⽅式 shared_ptr pint(new int(100)) 来创建⼀个 shared_ptr 并初始化,但是由于 shared_ptr 定义 的构造函数是 explicit 的,因此不能使⽤ shared_ptr pint = new int(100) 来创建⼀个 shared_ptr 并初始化,因为这种⽅式隐式要 求将⼀个普通的 int * 转换为 shared_ptr。 (四)常⽤操作 (四)常⽤操作 1. use_count 返回多少个智能指针指向该对象,主要⽤于调试。 2. unique 判断该智能指针是否独占该内存,如果该智能指针不指向任何对象,判断 unique 的时候也是假。 3. reset 不带参数时:放弃指针对对象的掌管权,重置为 nullptr 带参数时:参数⼀般是⼀个 new 的空间,相当于放弃指针对当前对象的掌管权,然后将指针指向 new 出来的空间。 4. * 解引⽤,可以获取指针指向的对象。 5. get 获取指针指针⾥保存的裸指针,⼀般⽤于⼀些接⼝需要使⽤到 C 语⾔指针的情况。 6. swap 交换两个智能指针所指的对象。 7. =nullptr 该智能指针指向 nullptr,代表解除对该对象的掌握权,引⽤计数将会减1,如果此时该内存空间的引⽤计数变为0,会同时释放该内存。 8. 指定删除器以及删除数组问题 指定删除器以及删除数组问题 智能指针能在⼀定时机帮我们删除所指向的对象,使⽤ delete 作为默认的资源析构⽅式,我们也可以指定⾃⼰的删除器取代系统提供的默 认删除器,当智能指针需要删除所指向的对象时,编译器就会调⽤我们提供的删除器。 shared_ptr 指定删除器的⽅法⽐较简单,⼀般只需要在参数中添加具体的删除器函数即可。如果提供了删除器,那么就需要⼿动删除资 源,否则会造成内存泄漏。删除器函数可以是函数,lambda 表达式,重载了 operator() 的类等。 还可以使⽤ default_delete 来做删除器,default_delete 是标准库⾥的⼀个模板类。如:shared_ptr p3(new A[10], default_delete()),这样就知道我们使⽤智能指针指向了⼀个对象数组,这样就可以正确释放了。 其实,使⽤ shared_ptr 指向对象数组不需要通过删除器的⽅式,只需要在定义 shared_ptr 时指为数组类型即可,如:shared_ptr p4(new A[10])。 额外说明:就算是两个 shared_ptr 指定了不同的删除器,只要他们指向的对象类型相同,那么这两个 shared_ptr 也是同⼀个类型,可以 放到同⼀个容器去,vectorptr> pvec{p1,p2}。 三 三
If you’re an experienced C++ programmer and are anything like me, you initially approached C++11 thinking, “Yes, yes, I get it. It’s C++, only more so.” But as you learned more, you were surprised by the scope of the changes. auto declarations, range-based for loops, lambda expressions, and rvalue references change the face of C++, to say nothing of the new concurrency features. And then there are the idiomatic changes. 0 and typedefs are out, nullptr and alias declarations are in. Enums should now be scoped. Smart pointers are now preferable to built-in ones. Moving objects is normally better than copying them. There’s a lot to learn about C++11, not to mention C++14. More importantly, there’s a lot to learn about making effective use of the new capabilities. If you need basic information about “modern” C++ features, resources abound, but if you’re looking for guidance on how to employ the features to create software that’s correct, efficient, maintainable, and portable, the search is more challenging. That’s where this book comes in. It’s devoted not to describing the features of C++11 and C++14, but instead to their effective application. The information in the book is broken into guidelines called Items. Want to understand the various forms of type deduction? Or know when (and when not) to use auto declarations? Are you interested in why const member functions should be thread safe, how to implement the Pimpl Idiom using std::unique_ptr, why you should avoid default capture modes in lambda expressions, or the differences between std::atomic and volatile? The answers are all here. Furthermore, they’re platform-independent, Standards-conformant answers. This is a book about portable C++.

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧