Templates for C++s
C++ templates are the mechanism for writing code that works with many types, resolved at compile time. Function and class templates are instantiated per type, concepts in C++20 constrain which types are allowed, and compile-time programming with constexpr and variadic templates moves work from runtime to the compiler for speed and stronger checks.
Stage: Generic Programming · Part of the C++ roadmap · Last reviewed 19 November 2026
What to learn in Templates
- Write a function template and a class template
- Constrain a template with a concept instead of unreadable errors
- Use constexpr functions to compute values at compile time
- Write a variadic template that forwards any number of arguments
- Explain template specialisation and when it is needed
Templates interview questions
What problem do C++20 concepts solve?
Before concepts, a template accepted any type and failed deep inside the code with long error messages. Concepts state requirements up front, such as being comparable or iterable, so mismatches fail early with clear errors and overloads can select by capability.
What is the difference between constexpr and const?
Const means a value will not change after initialisation, though it may be set at runtime. Constexpr means the value or function can be evaluated at compile time, so it can be used where constants are required, such as array sizes or template arguments.
Where Templates fits in the C++ roadmap
Templates belongs to the Generic Programming stage (advanced level), which takes about 28 hours. Templates.