In C++, the algorithm std::move() moves each element of a range into a destination. Moving can be much cheaper than copying for big objects such as strings or vectors.
After the move, the source elements still exist but their values are unspecified. Do not rely on what they hold. Note the algorithm takes three arguments. The one-argument std::move(x) from <utility> is a different function.
Syntax
Add #include <algorithm>, then call std::move(). Start with these two lines.
A unique_ptr owns its object and cannot be copied, so copy() would not compile.
move() hands each pointer to the destination.
A moved-from unique_ptr is guaranteed to be null, which is why the last line prints true.
Frequently Asked Questions
In C++, std::move() moves the elements of a range into another place instead of copying them. Include <algorithm> to use it.
Include <algorithm> for the three-argument range version. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns an iterator to the position after the last moved element in the destination.
The three-argument version in <algorithm> moves a whole range. The one-argument version in <utility> only turns an object into something that can be moved from.
The elements are valid but their values are unspecified. You can assign new values to them or destroy them.