Is unique_ptr
guaranteed to store nullptr
after move?
std::unique_ptr<int> p1{new int{23}};
std::unique_ptr<int> p2{std::move(p1)};
assert(!p1); // is this always true?
Yes, you can compare it to nullptr
after the move
and it is guaranteed to compare equal.
From §20.8.1/4 [unique.ptr]
Additionally,
u
can, upon request, transfer ownership to another unique pointeru2
. Upon completion of such a transfer, the following postconditions hold:
—u2.p
is equal to the pre-transferu.p
,
—u.p
is equal tonullptr
, and
...
(the member p
is described earlier as — a unique pointer is an object u
that stores a pointer to a second object p
)
::move
leaves an element is unspecified. I don't think there is any guarantee on the smart pointer's end either. That said, I'll leave it to the CPP experts to answer :) – Benjamin Gruenbaumunique_ptr
– Benjamin Gruenbaumstd::unique_ptr::operator:
andstd::unique_ptr::release
. – rubenvb