Do std::make_shared and std::make_unique have a "nothrow" version?
Asked Answered
P

1

18

For the new operator, we have the std::nothrow version:

std::unique_ptr<T> p = new(std::nothrow) T();

Do we have something like this for the std::make_shared or std::make_unique?

Peppermint answered 18/7, 2019 at 10:20 Comment(0)
N
14

No, we don't. Looking through the cppreference pages for make_unique and make_shared, we see that every version uses the default new overload.

It is not difficult to implement one, though, something like this:

template <class T, class... Args>
std::unique_ptr<T> make_unique_nothrow(Args&&... args)
    noexcept(noexcept(T(std::forward<Args>(args)...)))
{
    return std::unique_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
}

template <class T, class... Args>
std::shared_ptr<T> make_shared_nothrow(Args&&... args)
    noexcept(noexcept(T(std::forward<Args>(args)...)))
{
    return std::shared_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
}

(Note that this version of make_shared_nothrow does not avoid double allocation as make_shared does.) C++20 added many new overloads for make_unique, but they can be implemented in a similar way. Also, per comment,

Don't forget to check the pointer before using it, when using this version. — Superlokkus Jul 18 '19 at 10:46

Nugatory answered 18/7, 2019 at 10:31 Comment(7)
Don't forget to check the pointer before using it, when using this version.Spiculum
"we see that every version uses the default new overload." allocate_shared uses the provided allocator, not new. Also, if a user wants to use nothrow new, that probably means they don't want to turn on exception handling period.Medallion
@NicolBolas I absolutely can't imagine how allocate_shared got in there ... Thank you!Nugatory
@NicolBolas As for exception handling, I wanted to keep consistent with the expression new (nothrow) T(args...). So I propagate the exceptions thrown by the constructor.Nugatory
@L.F.: My point is that new(std::nothrow) will work in an exception-less build environment, reporting failure to allocate memory with nullptr. your suggestion will not, because the system trying to throw std::bad_alloc will fail, because exception handling is turned off.Medallion
@NicolBolas You are right. I have reverted my edit. Can you think of a way to efficiently implement make_shared_nothrow to avoid double allocation?Nugatory
@L.F.: Not really. There is no provision in the allocator model for failure to allocate to be signaled by returning null, so allocate_shared isn't available.Medallion

© 2022 - 2024 — McMap. All rights reserved.