C++ Templates: Deduction to Concepts

Loading...
ProPart of the Pro catalogue.
8 decks56 cards

About this topic

Templates are where C++ interviews stop being about syntax and start being about the compiler's model of your code. This topic builds that model one mechanism at a time, then asks you to use it the way an interviewer will: predict what the compiler deduces, repair code that does not compile, and implement the small library components that keep coming up.

The approach: a mechanism ladder with set pieces. Each deck isolates one mechanism — deduction, dependent names, packs, traits, SFINAE, concepts — in cards small enough to answer in two or three minutes, which is what makes them stick under spaced repetition. Each deck then ends in one or two starred set pieces: the interview-sized question that combines everything the deck just taught (a deduction guide, a dependent-name repair, tuple_apply, function_traits, an is_iterable detector, a subsumption-ordered overload set, a type-erased callable).

Eight decks, easy to hard.

  1. Template Foundations — function and class templates, alias templates, default and member template parameters, explicit template arguments, and why definitions live in headers.
  2. Deduction, Overloads, and Specialization — what T becomes for value, reference, and forwarding-reference parameters; array-extent deduction; how templates compete with non-template overloads; partial specialization, why function templates cannot have it, and deduction guides.
  3. Dependent Names and Instantiationtypename, .template, and this->; two-phase lookup; if constexpr discarding a branch; why a template body's error waits for instantiation.
  4. Non-Type and Variadic Templates — non-type parameters, parameter packs and sizeof..., recursive peeling and its fold-expression replacement, empty-pack identities, template-template parameters, and std::index_sequence.
  5. Type Traits and Unevaluated Expressions — building is_same, conditional_t, remove_cvref_t, and inline variable templates by hand; decltype(x) versus decltype((x)); std::declval; peeling a function type apart with partial specialization.
  6. SFINAE and Detection — substitution failure in the immediate context versus a hard error, std::enable_if_t in both its usable forms, std::void_t detection idioms, and a fallback overload that always stays viable. This deck is the legacy-reading skill: you will meet this code, even though you will write the next deck's version.
  7. Concepts and Requires — requires-expressions with simple, type, compound, and nested requirements; the requires-clause versus the requires-expression; rewriting an enable_if overload as a constrained one; and letting subsumption pick the most constrained candidate.
  8. Senior Template Design — replacing tag dispatch with if constexpr, constraining a CRTP mixin, hidden-friend operators and ADL, the overloaded visitor helper, controlling code bloat with explicit instantiation, an owning type-erased callable, and the judgment call between templates, concepts, virtual dispatch, and type erasure.

How the cards work. Most cards are exercises: you fill in a small region of a file that already contains the scaffolding and a doctest suite, and run it in the browser. Compile-time properties are checked with static_assert plus a matching runtime CHECK, so a passing run proves the property held during compilation. Where the point is a translation-unit rule or a compiler behaviour you cannot observe inside one file, the card asks a multiple-choice question with a teaching rationale on every option.

Exercises target GCC 14.3 in C++23 mode (-std=gnu++23) with doctest. Reference solutions are one click away, and hints move from a nudge to the exact construct.

Decks