What is std::move(), and when should it be used?
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
(And in addition to copy assignment operators, they have move assignment operators.)
Type &&
). std::move()
is a cast that produces an rvalue-reference to an object, to enable moving from it. 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.
You can use move when you need to "transfer" the content of an object somewhere else, without doing a copy (eg the content is not duplicated, that's why it could be use on some non-copyable objects, like an unique_ptr). It's also possible for an object to take the content of a temporary object without doing a copy (and save a lot of time), with std::move.
This link really helped me out :
http://thbecker.net/articles/rvalue_references/section_01.html
I'm sorry if my answer is coming too late, but I was also looking for a good link for the std::move, and I found the links above a little bit "austere".
This put the emphasis on r-value reference, in which context you should use them, and I think it's more detailed, that's why I wanted to share this link here.
1. "What is it?"
While std::move()
"looks like" a function - I would say it isn't really a function . It's sort of a converter between ways the compiler considers an expression's value.
2. "What does it do?"
The first thing to note is that std::move()
doesn't actually move anything .
If you've ever watched the animation series Bleach - it does the equivalent of Quincy Seele Schneider's Reishi softening (see also its use in this scene).
Seriously, though, it converts an expression from being an lvalue or pure rvalue (such as a variable you might be using for a long time yet, or a temporary you're passing around for a while, respectively) to being an xvalue. An xvalue tells the compiler:
You can plunder me, move anything I'm holding and use it elsewhere (since I'm going to be destroyed soon anyway)".
in other words, when you use std::move(x)
, you're allowing the compiler to cannibalize x
. Thus if x
has, say, its own buffer in memory - after std::move()
ing the compiler can have another object own it instead.
3. "When should it be used?"
Another way to ask this question is "What would I use/cannibalize an object's resources for?" well, if you're writing application code, you would probably not be messing around a lot with temporary objects created by the compiler. So mainly you would do this in places like constructors, operator methods, STL-algorithm-like functions etc. where objects get created and destroyed automagically alot. Of course, that's just a rule of thumb.
A typical use is 'moving' resources from one object to another instead of copying. @Guillaume links to this page which has a straightforward short example: swapping two objects with less copying.
template <class T>
swap(T& a, T& b) {
T tmp(a); // we now have two copies of a
a = b; // we now have two copies of b (+ discarded a copy of a)
b = tmp; // we now have two copies of tmp (+ discarded a copy of b)
}
using move allows you to swap the resources instead of copying them around:
template <class T>
swap(T& a, T& b) {
T tmp(std::move(a));
a = std::move(b);
b = std::move(tmp);
}
Think of what happens when T is, say, vector<int>
of size n. In the first version you read and write 3*n elements, in the second version you basically read and write just the 3 pointers to the vectors' buffers. Of course, class T needs to know how to do the moving; you should have a move-assignment operator and a move-constructor for class T for this to work.
上一篇: 什么是std :: promise?