A move constructor is a constructor that takes an rvalue reference to the same class (T(T&&)) and steals the resources of the source object instead of duplicating them. A copy constructor (T(const T&)) makes an independent duplicate. Moving is typically a few pointer assignments, while copying allocates and copies the data, so moves are much cheaper for types that own heap memory, file handles or sockets.
Move semantics were added in C++11 and are central to modern C++ (C++20 and C++23 build on them). This article shows both constructors on a small string class, when each one runs, and the rules that keep them correct.
The copy constructor runs when a new object is initialised from an existing lvalue, when you pass by value, or when you return a named object that cannot be elided. For a class that owns a raw buffer, it must perform a deep copy.
With only this constructor, String b = a; allocates a second buffer and copies every character. Both objects stay valid and independent.
An rvalue reference (T&&) binds to temporaries and to objects you explicitly mark as movable with std::move. The move constructor takes the source's pointer and leaves the source in a valid but unspecified, here empty, state.
Two details matter. First, the constructor is marked noexcept, because std::vector and other containers only use the move constructor during reallocation when it cannot throw; otherwise they fall back to copying to keep their strong exception guarantee. Second, the source is nulled so its destructor does not free the buffer that now belongs to the new object. delete[] nullptr is safe.
std::move does not move anything by itself; it only casts to an rvalue reference so overload resolution selects the move constructor. Since C++17, String d = make(); constructs directly into d through guaranteed copy elision, so neither constructor runs. After std::move(a), treat a as empty: you may assign to it or destroy it, but do not rely on its value.
| Aspect | Copy constructor | Move constructor |
|---|---|---|
| Signature | T(const T&) | T(T&&) noexcept |
| Source afterwards | Unchanged | Valid but unspecified, usually empty |
| Cost for heap data | Allocation plus copy | Pointer transfer |
| Typical trigger | Initialising from an lvalue | Temporaries, std::move, returning locals |
| Can throw | Yes (allocation) | Should not |
If a class defines a destructor, copy constructor, copy assignment, move constructor or move assignment, it should usually define all five. Declaring a copy constructor or destructor suppresses the implicit move operations, so a class with a hand-written destructor silently loses cheap moves unless you add them (or write = default).
The better design is the rule of zero: hold resources in members that already manage themselves, such as std::string, std::vector or std::unique_ptr, and write none of the five. The compiler-generated copy and move operations are then correct and noexcept where possible.
noexcept on the move constructor, which makes containers copy instead of move.std::move as if it still held its value.return std::move(local);, which disables copy elision and often makes things slower. Just return local;.const T&& or moving from a const object; the move degrades to a copy.For types that own heap memory, yes. For small trivially copyable types such as int or a struct of plain members, a move is the same as a copy, so there is no gain.
No. It is a cast to an rvalue reference. The actual transfer happens in the move constructor or move assignment operator that the cast selects.
Valid but unspecified. You can destroy it or assign a new value to it. Standard library types are guaranteed to be safe for those operations.
Only if you declare no copy operations, no move assignment and no destructor. Otherwise request it explicitly with T(T&&) = default;.
A copy constructor duplicates resources and leaves the source intact; a move constructor transfers them and leaves the source empty. Mark moves noexcept, null out the source, prefer the rule of zero, and let std::move express intent without using it on return values.