什么是std :: move(),什么时候应该使用它? [英] What is std::move(), and when should it be used?
问题描述
良好的链接。
http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors < br>
http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics
- 在C + +11,除了复制构造函数,对象可以有移动构造函数。
(除了复制赋值运算符,它们还有移动赋值操作符。) - 如果对象具有类型rvalue-reference(
Type&&
),则使用move构造函数而不是复制构造函数。 -
std :: move()
是一个转换,它生成对对象的右值引用,以允许从中移动。 - What is it?
- What does it do?
- When should it be used?
- In C++11, in addition to copy constructors, objects can have move constructors.
(And in addition to copy assignment operators, they have move assignment operators.) - The move constructor is used instead of the copy constructor, if the object has type "rvalue-reference" (
Type &&
). std::move()
is a cast that produces an rvalue-reference to an object, to enable moving from it.
这是一种新的C ++方法来避免拷贝。例如,使用移动构造函数, std :: vector
可能只是将其内部指针复制到新对象的数据,使移动的对象处于不正确的状态,避免复制所有数据。这将是C ++ - 有效。
尝试Google搜索移动语义,右值,完美转发。
Good links are appreciated.
http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics
It's a new C++ way to avoid copies. For example, using a move constructor, a std::vector
could just copy its internal pointer to data to the new object, leaving the moved object in an incorrect state, avoiding to copy all data. This would be C++-valid.
Try googling for move semantics, rvalue, perfect forwarding.
这篇关于什么是std :: move(),什么时候应该使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!