Week 3 · Move Semantics and Ownership
The first two rounds had you practicing recursion and language details; this one turns to a core mechanism of modern C++: move semantics. All five problems circle std::move, so one thing should be said up front: it moves nothing by itself. It just turns the expression into an rvalue; the move constructor and move assignment do the real work. This week's problems walk that whole chain: why unique_ptr can't be copied (ownership exists in exactly one copy), how a swap written with moves gets away with zero copies (that is exactly how std::swap is implemented), what a moved-from object has left (the "valid but unspecified" state), when std::move does nothing even though you wrote it (the rule that a handwritten destructor suppresses the move constructor). And the headliner asks: is moving really free? Short strings hand you a counterintuitive answer.
The format lineup stays mixed, as before. It opens with a multiple-choice warm-up about what compiles, then an online-judge problem — this time the judge checks your copy count, so let one stray copy sneak into your swap and it fails — then a single-choice free-response problem, then a root-cause hunt. The closer is an ungraded thought problem, left for you to take to Godbolt and see with your own eyes what code the compiler actually generated. Difficulty runs from one star to three, and the card order is the suggested tackling order. The first two rounds drew their problems from old books and friends' idle musings; this week we composed them ourselves around the tutorial's vol2 Move Semantics chapter — our first in-house special.
Where the problems come from: All five problems this week were composed by us around the tutorial's vol2 Move Semantics chapter. Problems 1 and 2 test points from cppreference's specifications of std::unique_ptr and std::swap (the copy constructor is deleted; from C++11 on, swap is implemented in terms of moves). Problem 3's wording, "valid but unspecified", comes from the C++ standard library's [lib.types.movedfrom] and lines up with Item 23 of Effective Modern C++. Problem 4 turns on C++ Core Guidelines C.21 (the rule of five) and the language rule that a declared destructor stops implicit moves from being generated. Problem 5's SSO (short string optimization) inline capacity — 15 characters in libstdc++ and MSVC, 22 in libc++ — is an implementation fact of the three mainstream standard libraries, taken apart in full in the Deep Dive into string article. Every judge case and every assertion inside the problems was verified by local testing on two compilers (GCC 16 / Clang 23).