Are there any downsides with using make_shared to create a shared_ptr(使用 make_shared 创建 shared_ptr 有什么缺点吗)
问题描述
使用 make_shared
代替 shared_ptr
有什么缺点吗.
Are there any downsides with using make_shared<T>()
instead of using shared_ptr<T>(new T)
.
Boost 文档 声明
有来自的重复请求工厂函数的用户创建一个给定类型的对象并返回一个 shared_ptr 给它.除了方便又时尚,这样的功能也是异常安全的相当快,因为它可以使用两者的单一分配对象及其对应的控件块,消除了一个重要的shared_ptr 的部分构造高架.这消除了其中之一主要效率投诉shared_ptr.
There have been repeated requests from users for a factory function that creates an object of a given type and returns a shared_ptr to it. Besides convenience and style, such a function is also exception safe and considerably faster because it can use a single allocation for both the object and its corresponding control block, eliminating a significant portion of shared_ptr's construction overhead. This eliminates one of the major efficiency complaints about shared_ptr.
推荐答案
我知道至少两个.
- 您必须控制分配.不是很大,但一些较旧的 api 喜欢返回必须删除的指针.
- 没有自定义删除器.我不知道为什么这不受支持,但事实并非如此.这意味着您的共享指针必须使用普通删除器.
相当薄弱的地方.所以尽量总是使用 make_shared.
Pretty weak points. so try to always use make_shared.
这篇关于使用 make_shared 创建 shared_ptr 有什么缺点吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 make_shared 创建 shared_ptr 有什么缺点吗


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07