【发布时间】:2018-10-18 00:39:54
【问题描述】:
我在一些代码中遇到过这种情况(为清楚起见,省略了细节):
std::vector<std::vector<int>> foo;
{
std::vector<int> bar = {42};
foo.push_back(std::move(bar)); // Hmmm...
} // Indicate `bar` is no longer needed.
std::move 对我来说似乎没有必要,但不是吗?这种行为与foo.push_back(bar); 有什么不同吗?如果元素不是int,而是像我的实际代码中的pcl::PointXYZ 这样的类怎么办?
更新:我修改了代码,更明确地指出bar在std::move之后没有使用,所以不存在非法访问等风险。
【问题讨论】:
标签: c++11 stl move-semantics