Templates in C++

From the C++ cheat sheet · Templates & Concepts · verified Jul 2026

Templates

Function and class templates.

cpp
// Quick Reference
template <typename T>
T max(T a, T b) { return a > b ? a : b; }

template <typename T>
class Box { T value; };
💡 Templates are instantiated per type at compile time - zero runtime overhead.
⚡ Template code usually lives entirely in headers (the definition must be visible to callers).
📌 Non-type parameters (int N) let you parameterize on compile-time values like sizes.
🟢 The compiler deduces T from the arguments - you rarely write maxOf<int>(...) explicitly.
templatesgenerics

More C++ tasks

Back to the full C++ cheat sheet