简体   繁体   English

如何组合std :: make_shared和new(std :: nothrow)

[英]How to combine std::make_shared and new(std::nothrow)

C++'s new has an option to return a null pointer instead of throwing a bad_alloc exception when an allocation failed. C ++的new有一个选项可以返回空指针,而不是在分配失败时抛出bad_alloc异常。

Foo * pf = new(std::nothrow) Foo(1, 2, 3);

(Yes, I understand this only prevents the new from throwing a bad_alloc; it doesn't prevent Foo's constructor from throwing an exception.) (是的,我理解这只会阻止new抛出bad_alloc;它不会阻止Foo的构造函数抛出异常。)

If you want to use a shared pointer instead of a raw pointer, you generally should use make_shared, as it gets clever about the allocation of the control block. 如果你想使用共享指针而不是原始指针,你通常应该使用make_shared,因为它对控制块的分配很聪明。

auto pf = std::make_shared<Foo>(1, 2, 3);

make_shared encapsulates the new, which makes it impossible(?) to choose the nothrow version. make_shared封装了new,这使得(?)无法选择nothrow版本。 So it seems you have to abandon make_shared and call new explicitly. 因此,您似乎必须放弃make_shared并明确调用new。

std::shared_ptr<Foo> pf(new(std::nothrow) Foo(1, 2, 3));

This eliminates the optimization of allocating the control block with the Foo, and the control block allocation could fail independently of the Foo allocation, but I don't want to focus on that. 这消除了使用Foo分配控制块的优化,并且控制块分配可能独立于Foo分配而失败,但我不想关注它。 Let's assume the control block is small so its allocation will never fail in practice. 让我们假设控制块很小,所以它的分配在实践中永远不会失败。 It's the failure to allocate space for the Foo that concerns me. 这是为我担心的Foo分配空间的失败。

Is there a way to get the single-allocation advantage of make_shared while retaining the ability to simply get a null pointer instead of a bad_alloc exception when allocating space for the Foo? 有没有办法获得make_shared的单一分配优势,同时保留在为Foo分配空间时简单地获取空指针而不是bad_alloc异常的能力?

它看起来像allocate_shared ,传入一个使用nothrow new的分配器应该为你做的伎俩。

Just have a custom nake_foo and catch the exception: 只需要一个自定义的nake_foo并捕获异常:

#include <memory>

struct Foo {
    Foo(int, int, int) { throw std::bad_alloc(); }
};

std::shared_ptr<Foo> make_foo(int a, int b, int c) {
    try { return std::make_shared<Foo>(a, b, c); }
    catch (const std::bad_alloc&) { return std::shared_ptr<Foo>(); }
}

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

相关问题 std :: make_shared和std :: make_unique是否有“nothrow”版本? - Do std::make_shared and std::make_unique have a “nothrow” version? 专攻std :: make_shared - Specializing std::make_shared std::make_shared 接口? - std::make_shared an interface? std::make_shared 在 std::apply 中 - std::make_shared inside std::apply 如何实现`new(std :: nothrow)`? - How is `new (std::nothrow)` implemented? 使用新宏的c ++ std :: make_shared - c++ std::make_shared with new macro 的std :: shared_ptr的 <type> (new DerivedType(…))!= std :: make_shared <type> (DerivedType(...))? - std::shared_ptr<type>(new DerivedType(…)) != std::make_shared<type>(DerivedType(…))? 如何使用std :: make_shared避免大内存分配 - How to avoid big memory allocation with std::make_shared 如何理解`std::make_shared<object> (“foo”)`?<div id="text_translate"><p> std::make_shared&lt;Object&gt;("foo")做什么?</p><pre> std::shared_ptr&lt;Object&gt; p1 = std::make_shared&lt;Object&gt;("foo");</pre><p> 我知道std::make_shared&lt;T&gt;是模板 class 并且我可以理解Object("foo")确实是临时的 object 。 但我无法理解std::make_shared&lt;Object&gt;("foo") 。</p><p> 有人可以逐步解释我创建的对象序列和它完成的操作吗?</p></div></object> - How to understand `std::make_shared<Object>(“foo”)`? 我如何理解 std::make_shared 的工作原理? - How do I understand how std::make_shared works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM