📖 Book · cpp17 · medium

🆕 std::variant: type-safe state machines

A closed sum type + std::visit = exhaustive, heap-free, no-vtable dispatch.

std::variant as a type-safe union and the std::visit + overloaded-lambda idiom for exhaustive dispatch, built into a runnable state machine. When to choose a closed variant over an open inheritance hierarchy, plus the access/valueless gotchas.

3 chapters▶ runnable code✦ in-depth animation
▶ See it animated:std::variant & std::visit

What this book covers

  1. 01A sum type for C++
  2. 02std::visit + the overloaded idiom — run it
  3. 03Access, exceptions & valueless

Read it in full — runnable C++ + step-through animations

Every chapter of std::variant: type-safe state machines ships complete, editable code you compile and run in the browser, plus the same in-depth animations as the lessons.