Key Takeaway
The copy constructor vs move constructor question comes down to what gets handed over. A copy constructor (T(const T&)) leaves the source untouched and duplicates everything, while a move constructor (T(T&&)) takes over the source’s resources (a heap pointer, for example) and leaves the source in a valid-but-unspecified state. Named lvalues pick the copy constructor; temporaries and objects cast with std::move pick the move constructor — and a common trap is that declaring a destructor alone suppresses the compiler-generated move constructor.
In C++ interviews and real-world code review, “which constructor gets called here” and “why did adding just a destructor make this slower” come up often. This post works through a minimal example to cover the copy/move difference, when each is called, and the Rule of 0/3/5.
Copy Constructor vs Move Constructor: What Gets Duplicated, and What Gets Handed Over?
One-line answer: Copy leaves the source intact and duplicates the contents (T(const T&)); move takes over the source’s resources (a heap pointer, a file handle, etc.) and leaves the source “valid but unspecified” (T(T&&)).
-
Per cppreference, a move constructor “takes over” another object’s resources and leaves that argument in a valid-but-unspecified state. Some types, like std::string or std::vector, are commonly left empty; others, like std::unique_ptr, are guaranteed by the standard to end up empty (null). (Source: Copy constructors, Move constructors)
-
Here’s copy vs move side by side:
| Aspect | Signature | Source state | Cost | Typical situation |
|---|---|---|---|---|
| Copy constructor | T(const T&) | Unchanged | Full content duplication | When the source still needs to be used |
| Move constructor | T(T&&) | Valid-but-unspecified | Just transfers a resource pointer (cheap) | Passing a temporary or an rvalue cast with std::move |
![Copy: objects a and b each own a heap buffer [A B C], so there are 2 buffers and the source is unchanged; move: object b takes over the original heap buffer and object a is left empty with ptr = nullptr, so there is 1 buffer](/_astro/copy-vs-move-buffers-en.Dh6G7fZU_2lrJwS.webp)
- A minimal example: a
Bufferclass that owns a heap buffer directly. The copy constructor allocates a new buffer and duplicates the contents withmemcpy; the move constructor just takes over the pointer and sets the source’s pointer back tonullptr.
#include <cstddef>
#include <cstring>
#include <iostream>
class Buffer {
public:
explicit Buffer(std::size_t n) : size_(n), data_(new char[n]{}) {}
// copy constructor: allocate a new heap buffer and duplicate the contents
Buffer(const Buffer& other) : size_(other.size_), data_(new char[other.size_]) {
std::memcpy(data_, other.data_, size_);
std::cout << "copy ctor\n";
}
// move constructor: take over the pointer and leave the source empty
Buffer(Buffer&& other) noexcept : size_(other.size_), data_(other.data_) {
other.size_ = 0;
other.data_ = nullptr;
std::cout << "move ctor\n";
}
~Buffer() { delete[] data_; }
std::size_t size() const { return size_; }
bool empty() const { return data_ == nullptr; }
private:
std::size_t size_;
char* data_;
};
int main() {
Buffer a(1024);
Buffer b = a; // lvalue → copy
std::cout << "a.size=" << a.size() << " b.size=" << b.size() << '\n';
Buffer c = std::move(a); // xvalue → move
std::cout << "a.empty=" << a.empty() << " c.size=" << c.size() << '\n';
}
Output:
copy ctor
a.size=1024 b.size=1024
move ctor
a.empty=1 c.size=1024
Which Constructor Gets Called When? (Return Values, Temporaries, std::move)
One-line answer: An rvalue of the same type (xvalue/prvalue) triggers move, an lvalue (a named object) triggers copy, and if there’s no move constructor, the const& copy constructor accepts rvalues too and copy is used as a fallback.
-
Between objects of the same type, overload resolution decides which one gets called. A named lvalue selects the copy constructor; an rvalue (a prvalue temporary, or an xvalue cast with std::move) selects the move constructor. If there’s no move constructor, though, the
const T© constructor can also bind to an rvalue, so copy is used as a fallback. -
The code below shows which constructor fires in seven different situations.
#include <iostream>
#include <utility>
struct Tracer {
Tracer() { std::cout << " default\n"; }
Tracer(const Tracer&) { std::cout << " copy\n"; }
Tracer(Tracer&&) noexcept { std::cout << " move\n"; }
};
Tracer make_prvalue() { return Tracer{}; } // return a temporary directly
Tracer make_named() { Tracer t; return t; } // return a named local
Tracer pick(bool f) { // two candidates → NRVO is hard
Tracer x, y;
if (f) return x;
return y;
}
void take(Tracer) {} // by-value parameter
int main() {
Tracer a;
std::cout << "1) Tracer b = a;\n";
Tracer b = a;
std::cout << "2) Tracer c = std::move(a);\n";
Tracer c = std::move(a);
std::cout << "3) Tracer d = make_prvalue();\n";
Tracer d = make_prvalue();
std::cout << "4) Tracer e = make_named();\n";
Tracer e = make_named();
std::cout << "5) Tracer f = pick(true);\n";
Tracer f = pick(true);
std::cout << "6) const Tracer g; Tracer h = std::move(g);\n";
const Tracer g;
Tracer h = std::move(g);
std::cout << "7) take(b); take(Tracer{});\n";
take(b);
take(Tracer{});
(void)c; (void)d; (void)e; (void)f; (void)h; // silence unused warnings
}
Output:
default
1) Tracer b = a;
copy
2) Tracer c = std::move(a);
move
3) Tracer d = make_prvalue();
default
4) Tracer e = make_named();
default
5) Tracer f = pick(true);
default
default
move
6) const Tracer g; Tracer h = std::move(g);
default
copy
7) take(b); take(Tracer{});
copy
default
- Case 3 (returning a temporary, a prvalue): neither copy nor move is called, thanks to guaranteed copy elision since C++17. (See: copy elision)
- Case 4 (returning a named local): no move is printed because NRVO (Named Return Value Optimization) kicked in — and NRVO is an optimization the standard does not guarantee. Compiling with
-fno-elide-constructorswould print move here instead. - Case 5 (two return candidates in pick): NRVO is hard to apply here, so implicit move on return kicks in and move is printed.
- Case 6 (const object + std::move): a
const Tracer&&can’t bind to the move constructor’sTracer&&, so it binds toconst Tracer&instead, and copy is printed.
- std::move isn’t a function that actually moves anything — it’s just a cast to an rvalue reference. For more detail, see C++ std::move, 복사를 줄이는 이유 (Korean).
What Are the Rule of 0/3/5, and When Do You Need to Define Them Yourself?
One-line answer: Leave resource management to member objects and you don’t need to write anything (Rule of 0); if you write any one of destructor/copy/move yourself, you need to review the rest too (Rule of 3/5).
-
The Rule of 0 means not writing any of the destructor, copy/move constructors, or copy/move assignment operators yourself. As long as members already manage their own resources correctly — std::string, std::vector, std::unique_ptr, and the like — the compiler-generated special member functions are enough.
-
The Rule of 3 says that if you define any one of the destructor, copy constructor, or copy assignment operator yourself, you should review the other two as well; the Rule of 5 extends that to the move constructor and move assignment operator too. Declaring a move constructor yourself causes the implicit copy constructor to become deleted, so if you still need copying, you have to define it explicitly.
-
If you declare any one of the destructor, copy constructor, copy assignment operator, or move assignment operator yourself, the compiler will not implicitly declare a move constructor. In that state, calling std::move falls back to the copy constructor, since there’s no move constructor to use. If you need the move, declare the move constructor explicitly as
= default.
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
struct Member {
Member() = default;
Member(const Member&) { std::cout << " Member copy\n"; }
Member(Member&&) noexcept { std::cout << " Member move\n"; }
};
struct RuleOfZero { // declares no special member functions
std::string name;
Member m;
};
struct DtorOnly { // only a destructor → no implicit move constructor
std::string name;
Member m;
~DtorOnly() {}
};
int main() {
std::cout << "RuleOfZero b = std::move(a);\n";
RuleOfZero a;
RuleOfZero b = std::move(a);
std::cout << "DtorOnly d = std::move(c);\n";
DtorOnly c;
DtorOnly d = std::move(c);
std::cout << std::boolalpha
<< "RuleOfZero nothrow move: "
<< std::is_nothrow_move_constructible_v<RuleOfZero> << '\n'
<< "DtorOnly nothrow move: "
<< std::is_nothrow_move_constructible_v<DtorOnly> << '\n';
}
Output:
RuleOfZero b = std::move(a);
Member move
DtorOnly d = std::move(c);
Member copy
RuleOfZero nothrow move: true
DtorOnly nothrow move: false

- When a std::vector reallocates, it moves existing elements into the new memory using
std::move_if_noexcept, which only uses the move constructor when it’s guaranteed not to throw — otherwise, if the type is copyable, it falls back to copying for safety. That’s why it’s a good idea to mark your move constructorsnoexcept. (As a side note, a reallocation invalidates all existing iterators.)
#include <iostream>
#include <vector>
int copies = 0, moves = 0;
struct NoexceptMove {
NoexceptMove() = default;
NoexceptMove(const NoexceptMove&) { ++copies; }
NoexceptMove(NoexceptMove&&) noexcept { ++moves; }
};
struct ThrowingMove {
ThrowingMove() = default;
ThrowingMove(const ThrowingMove&) { ++copies; }
ThrowingMove(ThrowingMove&&) { ++moves; } // no noexcept
};
template <typename T>
void grow(const char* name) {
copies = moves = 0;
std::vector<T> v;
v.reserve(4);
for (int i = 0; i < 4; ++i) v.emplace_back(); // fill capacity 4
v.emplace_back(); // 5th → reallocation, relocate the existing 4
std::cout << name << ": copies=" << copies << " moves=" << moves << '\n';
}
int main() {
grow<NoexceptMove>("noexcept move");
grow<ThrowingMove>("throwing move");
}
Output:
noexcept move: copies=0 moves=4
throwing move: copies=4 moves=0
[Interview two-liner] A copy constructor duplicates the entire payload to preserve the source’s state (
T(const T&)), while a move constructor only takes over ownership of the resource, avoiding duplicating the contents (T(T&&)). When writing a move constructor, mark itnoexceptso vector reallocation doesn’t silently fall back to copying, and prefer the Rule of 0 so the compiler generates everything for you whenever possible.
Shall We Check Your Understanding With a Q&A?
Q1. My code got slower after I only added a destructor — is copying happening?
A1. Yes, most likely. Once you declare a destructor yourself, the compiler no longer generates an implicit move constructor, so even std::move ends up calling the copy constructor instead of moving.
Q2. What happens if I pass a const object to std::move?
A2. A const T&& can’t bind to the move constructor’s T&& parameter, so it binds to const T& instead — meaning the copy constructor runs instead of the move constructor.
Q3. Can I keep using the source object after it’s been moved from?
A3. The source is left in a valid-but-unspecified state, so assigning a new value to it or destroying it is safe, but you should avoid reading its current value.
For more C++ interview questions, see C++ 기술면접 질문 10가지 (Korean).