Back to Basics: C++ Move Semantics - Andreas Fertig - CppCon 2022

C++ move semantics demystified: learn the basics, understand the pitfalls, and master the art of efficient code with expert Andreas Fertig's presentation at CppCon 2022.

Key takeaways
  • C++ move semantics: back to basics
  • 2 types of operations: copy/move and assign/move
  • Move semantics: swap contents, don’t copy
  • std::move is not a move, it’s a cast to rvalue reference
  • Copy operations are destructive, move operations are not
  • When to use move semantics: when you really need a performance win
  • Move semantics are not for everyone, it’s about implementing your own move operations
  • std::unique_ptr excels with move semantics
  • Need to provide own move operations for own classes
  • Implement move operations carefully to avoid performance regressions
  • std::move is not a magic solution, it’s a cast
  • Use std::forward when returning rvalue references from functions
  • Implement move semantics recursively in containers
  • Don’t touch a moved-from object, destroy it immediately
  • std::move does not guarantee performance, it’s just a cast
  • Use the “you know what you’re doing” rule for move semantics
  • Don’t use std::move for Lvalues, only for Rvalues