Advanced · Module 10
Move Semantics, Casts and Callables
Transfer ownership instead of copying, cast with intent, and store any callable with lambdas and std::function.
Copying a vector duplicates its elements; moving a vector with the default allocator is constant-time in the usual implementation because it can transfer its storage. This module builds on the value and ownership rules from earlier lessons, then covers move operations, the Rule of Five, the four named casts, lambdas, member pointers and callable wrappers. It ends by applying those ideas to a movable task type.
- 1Lvalues, Rvalues and Stealing Resources With MovePro — members onlyMove heap buffers instead of copying them, leave sources valid but empty, and make your vector and string moves non-throwing.11 min
- 2The Rule of Five and When to Default ItPro — members onlyCopy buffers deeply when duplication is wanted, move them cheaply on transfer, and express the choice with default and delete on MyVector and MyString.10 min
- 3Casts With Intent: static, dynamic, const and reinterpretPro — members onlyConvert with the narrowest cast that states intent, check polymorphic downcasts at runtime, and remove const only from values that began life mutable.9 min
- 4Lambdas: Captures, mutable and AlgorithmsPro — members onlyCapture local state by value or reference, mark stateful lambdas mutable, and pass them to sort, filter and wrapper callables.11 min
- 5Member Pointers and std::function CallablesPro — members onlyStore data and method pointers as values, invoke them on chosen objects, and erase callable types behind a common wrapper.10 min
- 6Project: A Movable Task TypePro — members onlyCombine an id with an erased callable into a move-only task, transfer it through a queue, and run it exactly once.11 min