std::unique_ptr<int> p1(new int);
std::unique_ptr<int> p2(new int);
p2=p1;
这里似乎 p1 不再是“唯一的”,因为 p2 也引用了它
这是合法的 c++ 吗? unique_ptr 有 copy_semantics 吗? 如果不是,并且它只有移动语义,那么在将 p1 分配给 p2 之后是否将其设置为 NULL ?
编辑:
好的,所以正确的版本是
p2=std::move(p1)
据此,在此分配之后,p1 无效?和 auto_ptr 的区别就在这里?明确指定所有权的转移比隐含的更安全,因为我猜想 auto_ptr 就是这种情况
最佳答案
关于c++ - std::unique_ptr 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622764/