Move assignment
Move assignment replaces an existing object's value by stealing resources from an expiring object of the same type. It is T& operator=(T&&). The left-hand side must release what it already owns; the right-hand side is left in a valid empty state.
Detailed Explanation
b = std::move(a); is assignment, so b already owns resources. The operator must:
- Guard against self-move (
&other == this), or use a sequence that is safe if both names are the same object. - Release
b's current resources (or swap them intoother). - Take
other's resources and emptyother. - Return
*this.
A common implementation is swap: swap the internals with other, then let other's destructor clean up what used to be b's. Self-move becomes a no-op swap. The source ends up with b's old value rather than empty, which is still a valid state. If you need the source to be empty after a self-move, write an explicit check.
The implicit move assignment move-assigns each member. It is generated under the same rules as the move constructor: user-declared copy operations, a destructor, or a user-declared move constructor can suppress it. When you write one special member, declare all five.
noexcept matters here for the same reason it matters on the move constructor. std::vector::operator= and many algorithms will move if move assignment is noexcept, and copy otherwise.
Move assignment should not leak if a later step throws. If you delete[] data_ and then something throws before you attach other.data_, you have a dangling pointer in *this. Steal into locals first, or swap.
Do not use a moved-from object except to assign to it, destroy it, or reset it. Standard containers promise "valid but unspecified". Your type should promise the same, plus whatever empty-state queries you want (empty(), size() == 0).
A class that is movable but not copyable (unique_ptr, thread, a socket wrapper) implements move assignment and deletes copy assignment. That is the type-system way to say "one owner".
Code Examples
#include <cstddef>
#include <utility>
class Buffer {
public:
explicit Buffer(std::size_t n) : data_(new char[n]), size_(n) {}
Buffer(Buffer&& other) noexcept
: data_(other.data_), size_(other.size_) {
other.data_ = nullptr;
other.size_ = 0;
}
Buffer& operator=(Buffer&& other) noexcept {
if (this == &other) return *this;
delete[] data_;
data_ = other.data_;
size_ = other.size_;
other.data_ = nullptr;
other.size_ = 0;
return *this;
}
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
~Buffer() { delete[] data_; }
private:
char* data_;
std::size_t size_;
};
int main() {
Buffer a(8);
Buffer b(2);
b = std::move(a); // move assignment
}#include <string>
#include <utility>
#include <vector>
int main() {
std::vector<std::string> a{"ada", "al"};
std::vector<std::string> b{"x"};
b = std::move(a); // b owns the heap buffer; a is empty
}Common Gotchas
Self-move without a guard
`delete[] data_; data_ = other.data_;` then `other.data_ = nullptr` frees the buffer and stores a pointer to freed memory when `this == &other`. Compare addresses or swap.
Delete-then-steal can leak on throw
if a later member's move throws, `data_` is already deleted. Prefer swap of a complete object, or move into locals first.
After move assignment the source is still alive
its destructor will run. Null pointers and empty sizes are the usual emptied state.
Declaring move assignment suppresses implicit copy assignment
if you still want copies, write them or `= default` them.