Hard15 minC++ Fundamentals
UpdatedAug 4, 2026
Edit

Move Semantics and Rule of Five

CONCEPTS:C++ Move Semantics

Question Variations

  • "What does `std::move` actually do?"
  • "What is the difference between copying and moving an object?"
  • "What are the Rule of Three, Rule of Five, and Rule of Zero?"
  • "Can you use an object after moving from it?"

Why This Is Asked

Move semantics are central to writing efficient, resource-safe modern C++. This question assesses whether you understand value categories, ownership transfer, and when custom special member functions are necessary versus when standard RAII types should manage resources for you.

Key Concepts

  • A move constructor or move assignment operation transfers resources from another object.
  • std::move casts an expression to an xvalue; it does not perform a move by itself.
  • A moved-from object remains valid but has an unspecified state.
  • Prefer the Rule of Zero; if a type directly owns a resource, consider the Rule of Five.

Question Variations

  • “What does std::move actually do?”
  • “What is the difference between copying and moving an object?”
  • “What are the Rule of Three, Rule of Five, and Rule of Zero?”
  • “Can you use an object after moving from it?”

Answers by Technology

+ Add Variant
C++Improve this answer ✏️

Expected Answer (C++23)

Move semantics let an object transfer ownership of a resource instead of allocating and copying it. std::move does not move data; it casts its argument so overload resolution can select a move constructor or move assignment operator when one is available.

The Rule of Zero is preferred: compose a type from RAII members such as std::string and std::vector, so the compiler can generate correct special member functions. A type that directly manages a resource may need to define or delete its destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator—the Rule of Five.

#include <string>
#include <utility>

class Report {
public:
    explicit Report(std::string title) : title_(std::move(title)) {}

    // Rule of Zero: std::string manages its own resource correctly.
private:
    std::string title_;
};

int main() {
    std::string source = "ready";
    std::string destination = std::move(source);
    // source is valid but its content is unspecified.
}

Why It Matters

Move operations avoid costly copies of buffers, containers, and handles while preserving clear ownership. Incorrect move implementations can leak, double-free, or leave objects in invalid states.

Common Mistakes

  • Assuming std::move performs a move: It only enables a move overload; the selected operation may still copy.
  • Reading a moved-from object’s old value: The object is valid but its content is unspecified unless the type documents otherwise.
  • Hand-writing special members for a type made of RAII members: This often adds bugs; prefer the Rule of Zero.

Follow-up Questions

  • Why should a move constructor often be noexcept? (Answer: Containers can then safely use it during reallocation instead of falling back to copying.)
  • What does the Rule of Five extend? (Answer: The Rule of Three by adding move construction and move assignment.)