I'm using unique_ptr
to manage some resources for safe destruction in every circumstance, etc.
void do_something(BLOB* b);
unique_ptr<BLOB> b(new_BLOB(20));
Is &*
much worse than get? e.g.
do_something(&*b);
or
do_someting(b.get());
both seem to work.
In short: Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.
When to use unique_ptr? Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for single resource its not possible to copy one unique_ptr to another.
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.
The operator*
of std::unique_ptr
has undefined behavior if no memory is owned by the unique_ptr
where as get()
will return nullptr
.
If the unique_ptr
could ever be empty I would suggest using get()
otherwise there is no difference.
&*b
is not necessarily equivalent to b.get()
. In order to be sure to recieve a pointer to the managed object, you want to use get()
.
There are two reasons:
operator*
on a unique_ptr
not managing any object is undefined behaviourT
may overload operator&
and as such you may not recieve a valid pointer to T
.The standard defines the bevaiour of operator*
of std::unique_ptr
:
typename add_lvalue_reference<T>::type operator*() const;
Requires:
get() != nullptr
Returns:
*get()
Thus, applying operator*
(from &*
) is not allowed if get()
returns nullptr
.
You will get the return value of any overloaded operator&
in the managed object if present. This can be circumvented by using std::addressof
but the undefined behaviour problem persists.
std::unique_ptr<T>::get()
will give you the address of the managed object or nullptr
if no object is managed. It is safer to use get()
and you have a predictable result.
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