We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Copy, Move, and the Rule of Zero
C++Understand copying, moving and the rule of zero. Track ownership and operation costs, and let member types manage resources where possible.
This track uses C++. You can make it your practice mode to browse related content across the site.
5 exercises
Explore freely. Sign in to keep your progress.
Your learning sequence
- 1 Not solved yet. Read payloads without copying their contents Practice · Easy
- 2 Not solved yet. When std::move selects a copy Practice · Medium
- 3 Not solved yet. Restore memberwise moves with the Rule of Zero Practice · Medium
- 4 Not solved yet. Implement copying and moving for an owned handle Practice · Hard
- 5 Not solved yet. What is left after you move from it Practice · Medium
Check yourself
4 questions · not saved while you're signed outUse these questions to check your understanding and compare explanations. Your score does not affect track completion.
With capacity reserved before either insertion, which constructor calls do these two insertions produce?
Choose one answer to reveal the explanations. Answers are kept only while this page is open.
#include <iostream>
#include <vector>
struct Tr {
Tr() { std::cout << "default" << std::endl; }
Tr(const Tr&) { std::cout << "copy" << std::endl; }
Tr(Tr&&) noexcept { std::cout << "move" << std::endl; }
};
int main() {
std::vector<Tr> v;
v.reserve(2);
std::cout << "push" << std::endl;
v.push_back(Tr{});
std::cout << "emplace" << std::endl;
v.emplace_back();
}