&*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:
- Calling
operator*
on a unique_ptr
not managing any object is undefined behaviour
- The managed object of type
T
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.
&*
with an iterator.