Is there a reason unique_ptr::reset
doesn't have overloads that take a const deleter&
and deleter&&
to match its constructors that take those as a second argument?
The stored deleter in unique_ptr
would be copy assigned or move assigned with the argument from reset
. If the deleter is noncopyable or nonmovable, calling the corresponding overload of the reset
wouldn't compile. This seems like it would be consistent behavior with the constructors.
An explicit delete for a unique_ptr would be reset() . But do remember that unique_ptr are there so that you don't have to manage directly the memory they hold. That is, you should know that a unique_ptr will safely delete its underlying raw pointer once it goes out of scope.
std::unique_ptr::resetDestroys the object currently managed by the unique_ptr (if any) and takes ownership of p. If p is a null pointer (such as a default-initialized pointer), the unique_ptr becomes empty, managing no object after the call.
Yes. Well the unique ptr has a function object that by default invokes delete on the pointed to object, which calls the destructor. You can change the type of that default deleter to do almost anything.
I thought about adding that but you can get the equivalent functionality with a move assignment operator:
ptr = unique_ptr<T, D>(new T(another_value), D(another_state));
So I opted for not saying the same thing with reset
in the interest of keeping the API reasonably small.
Update
And I live and learn...
The syntax can actually be much simpler than what I show above:
ptr = {new T(another_value), D(another_state)};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With