是否有使用make_shared创建一个shared_ptr的任何负面影响 [英] Are there any downsides with using make_shared to create a shared_ptr

查看:360
本文介绍了是否有使用make_shared创建一个shared_ptr的任何负面影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用 make_shared&LT任何缺点; T>()而不是使用的shared_ptr< T>(新T)

Boost文档状态


  

有过反复请求
  用户对于一个工厂函数
  创建特定类型的对象和
  返回一个shared_ptr它。除了
  便利性和风格,这样的功能
  也是异常安全
  相当快,因为​​它可以使用
  同时为一个单一的分配
  对象及其相应的控制
  块,消除了显著
  的shared_ptr的构造的一部分
  高架。这消除了一个
  重大的效率投诉
  shared_ptr的。



解决方案

我知道至少有两个。


  • 您必须在分配的控制权。没有一个大的真的,但一些旧的API一样返回指针,你必须删除。

  • 无定制删除。我不知道这是为什么不支持,但事实并非如此。这意味着您的共享指针必须使用香草的删除。

pretty薄弱点。所以尽量始终使用make_shared。

Are there any downsides with using make_shared<T>() instead of using shared_ptr<T>(new T).

Boost documentation states

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.

解决方案

I know of at least two.

  • You must be in control of the allocation. Not a big one really, but some older api's like to return pointers that you must delete.
  • No custom deleter. I don't know why this isn't supported, but it isn't. That means your shared pointers have to use a vanilla deleter.

Pretty weak points. so try to always use make_shared.

这篇关于是否有使用make_shared创建一个shared_ptr的任何负面影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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