Skip to content

Template Metaprogramming

Beginning of the lesson

Template Metaprogramming

Turning the compiler into a program that computes types and values before your code ever runs

Big Idea: A C++ template is not just a way to avoid copy-pasting a class for every type. It is a small functional programming language that runs inside the compiler. Template metaprogramming (TMP) means writing code whose job is to compute types and constant values during compilation, so that the machine code emitted for run time is already specialized, checked, and free of decisions that would otherwise cost time or safety at run time.
By the end you will be able to:
  • explain the two-phase model: what the compiler computes vs. what the CPU computes;
  • move computation to compile time with constexpr functions and consteval (C++20);
  • inspect and transform types with the <type_traits> library;
  • branch on types inside one function with if constexpr instead of tangled overloads;
  • enforce contracts at compile time with static_assert, and know the real trade-offs of TMP.

1. Two worlds: compile time and run time

Every C++ program lives in two worlds. In the compile-time world the compiler manipulates types and constant expressions, instantiates templates, and evaluates constexpr code. In the run-time world the CPU executes the machine code that survived compilation. Metaprogramming is the craft of doing as much useful work as possible in the first world so the second world has less to do.

You already know templates from the intermediate course: template<typename T> lets one definition serve many types. The key insight for TMP is that template instantiation is itself a computation. When you write std::vector<int>, the compiler runs the vector template with the argument int and produces a brand-new type. Feed a template different arguments and it computes different types — that is a pure function from types (and values) to types (and values), executed by the compiler.

Why bother? Three concrete pay-offs:

  • Speed: a value computed at compile time is a literal in the binary; the CPU never recomputes it.
  • Safety: a mistake caught during compilation can never reach a user. static_assert turns a logic error into a build error.
  • Generality without cost: one generic algorithm can specialize itself per type with zero run-time branching.
Rule of thumb: if an answer depends only on information the compiler already has (types, literals, sizes), compute it at compile time.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers