enable_shared_from_this和make_shared是否提供相同的优化 [英] Does enable_shared_from_this and make_shared provide the same optimization

查看:187
本文介绍了enable_shared_from_this和make_shared是否提供相同的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解, make_shared< T>(...)可以提供一些内存分配优化(它可以在与T类实例相同的内存块中分配引用计数器)

As I understand make_shared<T>(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T).

enable_shared_from_this是否提供相同的优化?因此:

Do enable_shared_from_this provides the same optimization? So:

class T : std::enable_shared_from_this<T> {};
...
auto t = std::shared_ptr<T>(new T);

与以下内容相同:

class T {};
...
auto t = std::make_shared<T>();

如果不考虑sizeof(T)。

If not take in account sizeof(T).

推荐答案


enable_shared_from_this是否提供相同的优化?因此:

Do enable_shared_from_this provides the same optimization? So:

否。从标准中的措辞可以看出, enable_shared_from_this< T> 具有 weak_ptr< T> 数据成员。这会向该类添加 weak_ptr< T> ,该类具有指向包含引用计数的控制块的指针。它不直接包含参考计数。包含引用计数的控制块仍然存在于对象外部。

No. As you can see from the wording in the standard, enable_shared_from_this<T> has a weak_ptr<T> data member. That adds a weak_ptr<T> to the class, which has a pointer to the control block that contains the reference counts. It doesn't contain the reference counts directly. The control block containing the reference counts still exists external to the object.

包含引用计数的控制块必须超过对象的寿命,以便其他用于引用该对象的weak_ptr 对象仍然可以访问控制块,以检查其是否已过期。

The control block containing the reference counts must outlive the object, so that other weak_ptr objects that used to refer to the object can still access the control block, to check whether it has expired.

在对象内部,对象将在销毁时被销毁,并且悬挂的 weak_ptr 不可能安全地确定对象是否已过期。从理论上讲,即使控制块的内存已被销毁,控制块的内存仍可以分配并继续使用,并且更新引用计数,但这看起来很丑陋(这意味着该对象不会被 delete ,则需要显式的析构函数调用和显式的 operator delete 调用来释放内存)。

If the control block was inside the object it would be destroyed when the object was destroyed, and it would not be possible for a dangling weak_ptr to safely determine if the object had expired. In theory the memory of the control block could remain allocated and still be used and the reference counts updated, even though the object they were part of was destroyed, but that seems pretty ugly (and it would mean the object would not be destroyed with delete, it would require an explicit destructor call and explicit operator delete call to free the memory).

如果拥有的 shared_ptr 是使用自定义删除器或自定义分配器创建的,则您也无法使用嵌入式控制块,因为大小这些对象中的一个将不会事先知道。在这种情况下,除了嵌入在 enable_shared_from_this< T> 基类中的一个控制块之外,您还需要分配一个外部控制块。甚至更多的空间。

You also couldn't use the embedded control block if the owning shared_ptr was created with a custom deleter or custom allocator, because the size of those objects would not be known in advance. In such cases you'd still need to allocate an external control block in addition to the one embeded in the enable_shared_from_this<T> base class, wasting even more space.

这篇关于enable_shared_from_this和make_shared是否提供相同的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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