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 有什么缺点吗
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
