liveBook · Manning

Move special member function

As explained in Learn C++ by Example

A move special member function is a move constructor or move assignment operator that transfers resources from another object.

A move special member function is a move constructor or move assignment operator that transfers resources from another object. Moving can avoid copies, but a move request may perform a copy when the type is not move-enabled. section 6.1.4

Keep these language rules in mind:

  • A user-declared destructor prevents C++ from generating move operations implicitly. section 6.1.4
  • Declaring the move constructor and move assignment operator as =default restores defaulted moves; deleting or declaring one special member can affect the others. section 6.1.4
  • The rule of five recommends deliberately declaring or deleting all five special members when resource ownership requires it; the rule of zero accepts suitable defaults. section 6.1.4

Checking a resource-owning Blob

Type traits can test whether Blob accepts rvalues, but is_move_constructible and is_move_assignable do not prove that a move constructor or move assignment operator exists. They may report true when copying through a const lvalue reference is possible. Static assertions can expose that Blob is neither copyable nor movable. section 6.1.4

How Learn C++ by Example covers it

How deep it goes
Explains implicit-generation rules, defaulting, deletion, and trait limitations, but stops short of implementing a resource transfer.
Examples
Uses a resource-owning Blob, static assertions, type traits, and vector push_back overloads to reveal move behavior.
What it assumes
Assumes special-member and rvalue-reference terminology while clarifying how traits interpret construction and assignment from rvalues.
What you can do after
Diagnose/debug with type traits whether a class is movable, and restore default moves when a destructor suppresses implicit generation.
How it connects
Connects move constructors, move assignment operators, destructors, copy operations, rvalue references, type traits, and the rule of five.

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.