Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::make_unique with null pointer?

I have not been able to find any documentation about what happens if I pass a null pointer to std::make_unique.

Is an exception thrown?

like image 914
Pietro Avatar asked Jul 12 '26 04:07

Pietro


1 Answers

std::make_unique forwards all the arguments passed to a matching target constructor. Therefore, if you pass nullptr, it will search for a constructor that accepts nullptr_t (or any matching constructor). If it is not there, it will fail to compile.

class A
{
public:
   A(void*) { ... } // could also be A(int*) or template <typename T> A(T*)
};

...
std::make_unique<A>(nullptr); // constructs an A by calling A(void*), passing nullptr.
like image 87
Michael Chourdakis Avatar answered Jul 13 '26 17:07

Michael Chourdakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!