使用 make_shared 创建 shared_ptr 有什么缺点吗 [英] Are there any downsides with using make_shared to create a shared_ptr

查看:44
本文介绍了使用 make_shared 创建 shared_ptr 有什么缺点吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 make_shared() 代替 shared_ptr(new T) 有什么缺点吗.

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 有什么缺点吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆