std::move(T) is a cast that allows you to under some situations, left and right hand side of an assignment to transfer ownership of state without performing a deep copy. For std::is_trivially_copyable objects, this still amounts to replicating the state. For more complex objects, it means state can 'move' between objects.
std::memcpy(Beg, End, Dest) is a function that allows you to replicate the state of an array of objects of a type such that std::is_trivially_copyable true.
std::copy(Beg, End, Dest) is a function that allows you to replicate the state of an array of objects of a type such that std::is_trivially_copyable is false.
std::move(Beg, End, Dest) is a function that allows you to perform std::move(T) over a range of elements, that implement move semantics.
So the difference is between:
#include <iostream>
#include <iterator>
#include <vector>
struct foo {
int x;
foo() : x(0) { }
};
struct bar {
std::vector<int> y;
bar() { }
bar(int sz) : y(sz, 0) { }
// The following 2 functions are not required (they have default implementations
// This is just to be clear what is happening under the hood to help explain when move works.
bar(bar &&other) { y = std::move(other.y); }
bar &operator =(bar &&other) { y = std::move(other.y); return *this; }
};
int main(int argc, char **argv) {
std::vector<foo> f(10);
std::vector<bar> b(10, bar(10000));
std::vector<foo> f2;
std::vector<bar> b2;
f2.reserve(f.size());
b2.reserve(b2.size());
bool doCopy = false;
if (doCopy) {
std::copy(f.begin(), f.end(), std::back_inserter(f2));
std::copy(b.begin(), b.end(), std::back_inserter(b2));
} else {
std::move(f.begin(), f.end(), std::back_inserter(f2));
std::move(b.begin(), b.end(), std::back_inserter(b2));
}
std::cout << b[0].y.size() << " " << b2[0].y.size() << std::endl;
}
You can safely std::copy or std::move the content of either of f -> f2 or b -> b2.
In the case of f -> f2, it doesn't matter. You copy the elements either way.
In the case of b -> b2 copy and move do exactly as they imply.
Copy makes a copy. Both 'b' and 'b2' contain a copy of the information (10 * 10000 * 2 ints in memory).
Move moves the data from 'b' to 'b2'. Only 'b2' contains a copy of the information now because each vector in bar was "moved" to the associated vector in the new set of objects. (10 * 10000 ints in memory)