简体   繁体   English

在构造对象时是否有任何理由不使用std :: make_shared?

[英]Is there any reason not to use std::make_shared when constructing objects?

I can't think of any situation where 我想不出任何情况

std::shared_ptr<Object> obj(new Object("foo", 1));

would be preferred to 会优先考虑

auto obj = std::make_shared<Object>("foo", 1);

The latter always results in better locality and reduces memory fragmentation. 后者总是会产生更好的局部性并减少内存碎片。 Is there any situation, where you would prefer (or be forced) to use the first form, except interfacing with code returning raw pointers? 是否有任何情况,您希望(或被迫)使用第一种形式,除了与返回原始指针的代码接口?

The latter always results in better locality and reduces memory fragmentation. 后者总是会产生更好的局部性并减少内存碎片。

Not always . 总是 An implementation is encouraged to use a single allocation for the reference counted object and the reference count, but it is not required to do so. 鼓励实现对引用计数对象和引用计数使用单个分配,但不要求这样做。

Why might you not want to use std::make_shared ? 为什么你不想使用std::make_shared Consider the case where you have a large dynamically allocated object that you want owned by std::shared_ptr and you know that there will be weak references to this object that are likely to outlive the strong references to the object (perhaps there are many weak references and only one or two short-lived strong references). 考虑这样一种情况:你有一个你想要由std::shared_ptr拥有的大型动态分配对象,并且你知道对这个对象的弱引用可能比对象的强引用更长(可能有许多弱引用并且只有一两个短命的强引用)。

In this case, std::make_shared would be a poor choice, assuming it allocates a single block that owns both the object and the reference count: the allocated block (which is large, remember) cannot be destroyed until there are no strong or weak references left to the object. 在这种情况下, std::make_shared将是一个糟糕的选择,假设它分配一个拥有对象和引用计数的块:分配的块(很大,记得)在没有强或弱之前不能被销毁引用留给对象。

If you were to dynamically allocate the object yourself and pass the pointer to the std::shared_ptr constructor, the memory occupied by the object could be released as soon as there are no strong references remaining, even if there are still weak references. 如果您自己动态分配对象并将指针传递给std::shared_ptr构造函数,则只要没有强引用,即使仍有弱引用,也可以释放对象占用的内存。

Modern IDEs have "Find usages" feature. 现代IDE具有“查找用法”功能。 If you use std::make_shared to construct the object, when you search for the places where the constructor of that object is called, IDE won't show up the places where std::make_shared is used. 如果使用std::make_shared构造对象,则在搜索调用该对象的构造函数的位置时,IDE将不会显示使用std::make_shared位置。

If you are constructing an object that will have multiple entities sharing in the lifetime of the object, then yes, you want to prefer to use std::make_shared where possible. 如果您构建的对象将在对象的生命周期中共享多个实体,那么是的,您希望尽可能使用std :: make_shared。 Some places it might not be possible is if you obtain the pointer from someone else; 有些地方可能是不可能的,如果你从别人那里获得指针; for example, a factory might return a raw pointer or a std::unique_ptr that you would like to store in a shared_ptr, thus preventing use of make_shared. 例如,工厂可能会返回您希望存储在shared_ptr中的原始指针或std :: unique_ptr,从而阻止使用make_shared。

The question in the title is a bit different; 标题中的问题有点不同; there are plenty of reasons you might not want to use shared_ptr for your objects at all. 有很多原因你可能根本不想将shared_ptr用于你的对象。 You should only use shared_ptr if the lifetime is actually shared by multiple objects. 如果生命周期实际上由多个对象共享,则应该只使用shared_ptr。 Otherwise, prefer stack allocating the object or using unique_ptr. 否则,首选stack分配对象或使用unique_ptr。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM