【发布时间】:2014-07-26 12:57:00
【问题描述】:
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?
【问题讨论】:
-
技术上未指定
::move离开元素的状态。我认为智能指针的末端也没有任何保证。也就是说,我将把它留给 CPP 专家来回答:) -
等等,没关系,是的。 Release 明确地将其设置为 nullptr。
unique_ptr保证了这一点 -
gcc 4.8.1 这里,p1 在 std::move 之后为空
-
在
std::unique_ptr::operator:和std::unique_ptr::release上查看任何参考资料。 -
只看移动语义...
标签: c++ c++11 move-semantics unique-ptr