Move Semantics & std::move
The idea
Copying a big object duplicates all its data. But if the source is about to disappear anyway (a temporary, or something you're done with), we can steal its resources instead: copy a pointer, set the source's pointer to null. That's a move — O(1) instead of O(n).
lvalues and rvalues (simplified)
| lvalue | rvalue | |
|---|---|---|
| Has a name / address | yes (x, v[0]) | no (temporaries: a + b, make_vector(), 42) |
| Can be moved from automatically | no | yes |
Rvalue references T&&
Bind to rvalues — "this object can be pillaged":
Buffer(Buffer&& other) noexcept
: data_(other.data_), size_(other.size_) {
other.data_ = nullptr; // leave the source empty but valid
other.size_ = 0;
}
std::move (<utility>)
Doesn't move anything! It's a cast to T&& that says "I'm done with this; you may move from it":
std::vector<int> a(1'000'000);
std::vector<int> b = std::move(a); // b takes a's buffer; a is now empty (valid but unspecified)
⚠️ After moving, only assign to it or destroy it — don't rely on its value.
Where moves happen automatically
- Returning local objects (plus RVO/copy elision often avoids even the move)
v.push_back(make_string())— temporaries- Standard containers reallocating (only if your move constructor is
noexcept✅)
Sink parameters
Take by value, then move into place:
Customer(std::string name) : name_(std::move(name)) {}
Callers passing a temporary pay only moves; callers passing an lvalue pay one copy.
Move-only types
std::unique_ptr, std::thread, std::fstream can be moved but not copied.
Rules ✅
- Mark move operations
noexcept - Don't
std::moveaconstobject (it silently copies) - Don't
return std::move(local);— it blocks copy elision
Example
#include <iostream>
#include <string>
#include <utility>
#include <vector>
class Buffer {
public:
explicit Buffer(std::size_t n) : data_(new int[n]{}), size_(n) {}
~Buffer() { delete[] data_; }
Buffer(const Buffer& o) : data_(new int[o.size_]{}), size_(o.size_) {
for (std::size_t i = 0; i < size_; ++i) data_[i] = o.data_[i];
std::cout << " copy (" << size_ << " ints duplicated)\n";
}
Buffer(Buffer&& o) noexcept : data_(o.data_), size_(o.size_) { // steal the resources
o.data_ = nullptr;
o.size_ = 0;
std::cout << " move (just pointers swapped)\n";
}
Buffer& operator=(Buffer o) noexcept { // handles both copy and move assignment
std::swap(data_, o.data_);
std::swap(size_, o.size_);
return *this;
}
std::size_t size() const { return size_; }
private:
int* data_;
std::size_t size_;
};
Buffer make_buffer() {
Buffer b(1000);
return b; // no std::move: copy elision or an automatic move
}
class Customer {
public:
explicit Customer(std::string name) : name_(std::move(name)) {} // sink parameter
const std::string& name() const { return name_; }
private:
std::string name_;
};
int main() {
Buffer a(1'000'000);
std::cout << "Buffer b = a;\n";
Buffer b = a;
std::cout << "Buffer c = std::move(a);\n";
Buffer c = std::move(a);
std::cout << "sizes: a=" << a.size() << " b=" << b.size() << " c=" << c.size() << '\n';
std::cout << "Buffer d = make_buffer();\n";
Buffer d = make_buffer();
std::cout << "d.size() = " << d.size() << '\n';
std::vector<Buffer> list;
list.reserve(2);
std::cout << "push_back(Buffer(10)):\n";
list.push_back(Buffer(10)); // temporary: moved automatically
std::cout << "push_back(std::move(b)):\n";
list.push_back(std::move(b));
std::string long_name(40, 'A');
Customer cust(std::move(long_name)); // no copy of the characters
std::cout << "customer name length " << cust.name().size()
<< ", moved-from string is now empty? " << std::boolalpha << long_name.empty() << '\n';
return 0;
}
/* Output:
Buffer b = a;
copy (1000000 ints duplicated)
Buffer c = std::move(a);
move (just pointers swapped)
sizes: a=0 b=1000000 c=1000000
Buffer d = make_buffer();
d.size() = 1000
push_back(Buffer(10)):
move (just pointers swapped)
push_back(std::move(b)):
move (just pointers swapped)
customer name length 40, moved-from string is now empty? true
*/