Value Categories and References
Move Reference Transfer
std::move turns a named object into an rvalue expression so ownership can be transferred.
Move Reference Transfer
move_reference_transfer.cpp
#include <iostream>
#include <memory>
int main() {
int amount = ;
std::unique_ptr<int> source = std::make_unique<int>(amount);
std::unique_ptr<int> target = std::move(source);
std::cout << "sourceEmpty=" << (source == nullptr) << std::endl;
std::cout << "targetValue=" << *target << std::endl;
return 0;
}
#include <iostream>
#include <memory>
int main() {
int amount = ;
std::unique_ptr<int> source = std::make_unique<int>(amount);
std::unique_ptr<int> target = std::move(source);
std::cout << "sourceEmpty=" << (source == nullptr) << std::endl;
std::cout << "targetValue=" << *target << std::endl;
return 0;
}
#include <iostream>
#include <memory>
int main() {
int amount = ;
std::unique_ptr<int> source = std::make_unique<int>(amount);
std::unique_ptr<int> target = std::move(source);
std::cout << "sourceEmpty=" << (source == nullptr) << std::endl;
std::cout << "targetValue=" << *target << std::endl;
return 0;
}
move expression
`std::move` does not move by itself; it marks an object as movable so a move constructor or move assignment can use it.
moved-from owner
A moved-from `std::unique_ptr` becomes empty after its ownership is transferred.