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?
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. From C++2011 Standard Section 20.7.1/4:
Yes, you can compare it to
nullptr
after themove
and it is guaranteed to compare equal.From §20.8.1/4 [unique.ptr]
(the member
p
is described earlier as — a unique pointer is an objectu
that stores a pointer to a second objectp
)