移动后是否需要重置std :: list? [英] Is it necessary to reset std::list after been moved?
本文介绍了移动后是否需要重置std :: list?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
std::list some_data;
...
std::list new_data = std::move(some_data);
some_data.clear();
...
问题是 some_data.clear( )
是必要的吗? (记录下来, some_data
将来会被重用)
The question is whether some_data.clear()
is necessary? (for the record, some_data
will be reused in the future)
推荐答案
是的,这是必需的。
只有从标准std智能指针移出后,才能保证其处于默认构造状态。
Only the std smart pointers are guaranteed to be in a default constructed state after being moved from.
容器处于有效但未指定的状态。这意味着您只能在没有前提条件的情况下调用成员函数,例如 clear
,将对象置于完全已知的状态。
Containers are in an valid, but unspecified state. This means you can only call member functions without preconditions, e.g. clear
, that put the object in a fully known state.
这篇关于移动后是否需要重置std :: list?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文