Moving semantics does not return a “fake” copy. Instead, in a copy operation, it gives copy permission to delete the original copy.
A motivating example can be instructive:
std::vector<int> bigData;
Without move semantics, you need to copy bigData
to paste it into listOfData or manually flip it with swap()
into a new empty vector that is in listOfData. But with the move semantics, the aka move constructor std::vector
constructor, called by push_back
as it is copied to the new data, has permission to delete the old bigData
content - this is allowed to steal the bigData
internal pointer and reset bigData
for an empty vector.
Movement semantics are usually faster than COW semantics, as they do not need to maintain reference counting for read-only shared data. They are, however, more limited - you cannot create multiple reference counts for your data using move semantics; you can easily and conveniently mix data between containers.
Also note that both the latest versions of GCC and Microsoft Visual C ++ support rvalue references and move semantics (GCC with --std=c++0x
), so there is no reason not to start using them today.
source share