是unique_ptr保证存储nullptr后移动? [英] Is unique_ptr guaranteed to store nullptr after move?

查看:187
本文介绍了是unique_ptr保证存储nullptr后移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

移动后 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 pointer u2. Upon completion of such a transfer, the following postconditions hold:
u2.p is equal to the pre-transfer u.p,
u.p is equal to nullptr, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆