Skip to content
UPEEC

Templates and Generic Programming

Write code once for many types — templates, concepts, type traits and compile-time computation.

Advanced
Comments 0

This is an overview — pick a sub-topic below.

Sub-topics (16)

Class Templates

Classes parameterised by types or values — `std::vector<T>`, `std::array<T, N>`.

Intermediate♥ 0🔖 0💬 0

Partial Specialization

Specialise a class template for a family of types (e.g. all pointers).

Advanced♥ 0🔖 0💬 0

Variadic Templates

Templates accepting any number of type arguments (parameter packs).

Advanced♥ 0🔖 0💬 0

Concepts

Named requirements on template arguments (C++20) with clear error messages.

Intermediate♥ 0🔖 0💬 0

Constraints and requires

`requires` clauses and expressions specify exactly what a template needs.

Advanced♥ 0🔖 0💬 0

SFINAE

"Substitution Failure Is Not An Error": invalid template substitutions remove an overload instead of failing.

Expert♥ 0🔖 0💬 0

Type Traits

Compile-time queries and transformations of types (`<type_traits>`).

Advanced♥ 0🔖 0💬 0

Fold Expressions

Apply an operator over a parameter pack (C++17).

Advanced♥ 0🔖 0💬 0

CRTP

Curiously Recurring Template Pattern: a class derives from a template of itself for static polymorphism.

Expert♥ 0🔖 0💬 0

constexpr

Values and functions that can be evaluated at compile time.

Intermediate♥ 0🔖 0💬 0

consteval

Functions that must run at compile time (C++20) — "immediate functions".

Advanced♥ 0🔖 0💬 0

constinit

Requires a static variable to be initialised at compile time (C++20), avoiding the static initialisation order fiasco.

Advanced♥ 0🔖 0💬 0

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.