Move Semantics in C++

Idea: Move semantics let objects transfer ownership of resources instead of copying them, improving performance.

Key points:

  • Introduced in C++11 to avoid unnecessary deep copies.
  • A move constructor or move assignment operator takes an rvalue reference (T&&).
  • After a move, the source object remains valid but unspecified.
  • Used implicitly for temporaries or explicitly via std::move().
  • Copy duplicates, move transfers.

Example

std::string a = "hello";
std::string b = std::move(a); // moves, doesn't copy

Implementation Sketch