是unique_ptr保证存储nullptr后移动? [英] Is unique_ptr guaranteed to store nullptr after move?
问题描述
移动后 unique_ptr
保证存储 nullptr
?
std::unique_ptr<int> p1{new int{23}};
std::unique_ptr<int> p2{std::move(p1)};
assert(!p1); // is this always true?
推荐答案
是的,您可以将其与<$ c $在移动
后保证比较相等。
Yes, you can compare it to nullptr
after the move
and it is guaranteed to compare equal.
§20.8.1/ 4 [unique.ptr]
From §20.8.1/4 [unique.ptr]
此外,
/ code>可以根据请求将所有权转移给另一个唯一指针
u2
。完成此类转移后,以下后置条件成立:
-u2.p
等于转移前,>
,<$> $ <$> $
c> ,以及
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
...
> p 之前被描述为< em>唯一指针是存储指向第二对象的指针的对象 u
c> p )
(the member p
is described earlier as — a unique pointer is an object u
that stores a pointer to a second object p
)
这篇关于是unique_ptr保证存储nullptr后移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!