Skip to content
Module 4

Templates & Generic Programming

Write genuinely generic, reusable, zero-overhead abstractions using templates, concepts and compile-time programming, without once copying and pasting a function.

Template Fundamentals

  1. Function Templates & Type Deduction

    Learn how to write functions that work with any type. Understand template argument deduction, non-type parameters, and why templates live in headers (it is not to be sociable).

  2. Class Templates & CTAD

    Build generic data structures with class templates. Understand member definitions, default arguments, and how C++17's CTAD lets you omit template arguments.

Advanced Templates

  1. Template Specialisation & Tag Dispatch

    Master full and partial specialisation, learn when to prefer overloading, and discover tag dispatch and if constexpr as alternatives for compile-time branching.

  2. Variadic Templates & Fold Expressions

    Handle any number of template arguments with parameter packs. Master fold expressions for elegant, recursion-free pack processing.

  3. SFINAE, Type Traits & enable_if

    Understand the Substitution Failure Is Not An Error principle, use type traits for compile-time introspection, and constrain templates with enable_if.

Modern Generic Programming

  1. C++20 Concepts & Constraints

    Define precise requirements on template arguments using concepts. Write cleaner generic code with requires clauses, abbreviated function templates, and standard library concepts.

  2. constexpr, consteval & Compile-Time Programming

    Move computation from runtime to compile time using constexpr, consteval and constinit. Build lookup tables, validate configurations, and use constexpr containers.

  3. Template Metaprogramming Patterns

    Master advanced template patterns: CRTP for static polymorphism, policy-based design for flexible composition, type erasure for runtime generics, and guidelines for when to use templates vs virtual dispatch.