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
?
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
?
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