Move constructor
The move constructor builds a new object by stealing resources from an expiring object of the same type. Its signature is T(T&&). After it runs, the source is still a live object, but its value is unspecified and safe only to assign to or destroy.
Detailed Explanation
Copying a std::vector of a million ints allocates and copies a million ints. If the source is a temporary that is about to die, that work is wasted. The move constructor takes the source's buffer pointer, size, and capacity, and leaves the source empty. The new object owns the buffer. The temporary's destructor then delete[]s a null pointer, which is a no-op.
The compiler generates a move constructor if you have not declared any of the other special members that suppress it, and if every member and base is movable. The implicit move constructor move-constructs each member. std::string and std::unique_ptr members make the whole class movable for free.
You write one when you wrote a destructor that owns a raw resource. The pattern is:
- Take the pointers and handles from
other. - Set
other's pointers tonullptr(or its handles to "empty"). - Do not
deleteanything in the move constructor; you now own those resources, andothermust not.
Mark it noexcept if it cannot throw. std::vector will move on reallocation only when it knows the move cannot throw; otherwise it copies. A move constructor that only copies pointers can always be noexcept.
The source remains a valid object. For standard library types the state is "valid but unspecified": size() is usually 0, but you must not depend on contents. You may assign to the source, destroy it, or call functions that set a known state. You may not assume other.data() still points at the old buffer.
T(T&&) binds to rvalues: temporaries, and named objects you cast with std::move. It does not bind to const lvalues. A const T cannot be moved from in the stealing sense; overload resolution falls back to the copy constructor. That is why const std::unique_ptr<int> is effectively stuck.
Returning a local by value already treats the local as an rvalue. Write return local;, not return std::move(local);. The extra std::move can block copy elision.
A moved-from unique_ptr is empty. A moved-from thread is not joinable. Your type should pick an equally simple empty state and document it.
Code Examples
#include <cstddef>
#include <iostream>
#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; // source destructor must not delete[] this
other.size_ = 0;
}
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
Buffer& operator=(Buffer&&) = delete;
~Buffer() { delete[] data_; }
std::size_t size() const { return size_; }
private:
char* data_;
std::size_t size_;
};
int main() {
Buffer a(8);
Buffer b = std::move(a); // move constructor
std::cout << b.size() << ' ' << a.size() << '\n'; // prints: 8 0
}#include <string>
#include <utility>
#include <vector>
std::vector<std::string> makeLines() {
std::vector<std::string> lines{"boot", "ready"};
return lines; // move or elide; do not std::move(lines)
}
int main() {
auto v = makeLines();
(void)v;
}Common Gotchas
Forgetting to null the source pointer
both destructors `delete[]` the same buffer. The move constructor must leave the source empty.
A throwing move constructor makes `vector` copy
omit `noexcept` and reallocations copy every element. Mark moves `noexcept` when they only steal pointers.
Moved-from is not destroyed
the source's destructor will still run. Empty it; do not think of move as "destroying in place".