Skip to main content

Checkpoint: One Function, Three Types

intermediate35 min readLesson 106 of 204

std::visit + if constexpr formatting across a variant — the modern dispatch idiom under exam conditions.

describe is the module's exam: a three-way variant, one generic lambda, and compile-time branch selection with if constexpr.

The design decisions you are rehearsing:

  • the visitor handles every alternative — add a fourth alternative later and the lambda must grow or fail to compile
  • std::decay_t<decltype(x)> names the held type inside the generic lambda
  • the double case needs std::fixed with precision(2) BEFORE the value is streamed — output state is order-sensitive

If the int case passes but doubles print as 3.5, you have rediscovered why formatting state lives on the stream, not in the value.