█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/C++/Templates and C++20 Concepts
55 minAdvanced

Templates and C++20 Concepts

After this lesson, you will be able to: Write function and class templates, use template specialization, and apply C++20 concepts.

Templates are how C++ does generics. They generate per-type optimal code at compile time, zero overhead, ugly errors, immense power.

Prerequisites:Smart pointers and RAII

Function templates

Same function, multiple types, compiler stamps out a version per type used.

tsx
// Generic max function
template <typename T>
T max_of(T a, T b) {
return (a > b) ? a : b;
}
max_of(3, 7); // T deduced as int
max_of(3.14, 2.71); // T deduced as double
max_of(std::string("a"), std::string("b")); // T deduced as string
// Explicit type parameter (if you can't deduce)
int x = max_of<int>(3, 7);
// auto return type (C++14+)
template <typename T, typename U>
auto add(T a, U b) {
return a + b; // return type deduced from a + b
}

Class templates + specialization

Build your own generic container.

tsx
// Generic stack
template <typename T>
class Stack {
std::vector<T> data_;
public:
void push(const T& v) { data_.push_back(v); }
void pop() { data_.pop_back(); }
T& top() { return data_.back(); }
bool empty() const { return data_.empty(); }
};
Stack<int> intStack;
Stack<std::string> strStack;
// Template specialization: provide a different impl for one type
template <>
class Stack<bool> {
// Pack bools as bits for memory efficiency
// ... custom impl
};

C++20 Concepts — constrain templates

Before concepts, template errors were impossible to read. Concepts fix that.

tsx
#include <concepts>
// Old (C++17): SFINAE, ugly errors
// New (C++20): concepts, clear errors
template <typename T>
requires std::integral<T>
T square(T x) { return x * x; }
square(5); // OK, int is integral
// square(3.14); // ERROR, clear: '3.14 is not integral'
// Shorthand: requires-clause via auto
auto square2(std::integral auto x) { return x * x; }
// Define your own concept
template <typename T>
concept Numeric = std::integral<T> || std::floating_point<T>;
template <Numeric T>
T scale(T x, T factor) { return x * factor; }

💡 Templates compile slowly + error verbosely

Heavy template use slows builds significantly (per-type instantiation). Pre-concepts errors could be 1000+ lines of compiler vomit. Use concepts when available. For library code: templates are essential. For app code: prefer normal polymorphism unless generics genuinely help.

Quick Check

What's the main benefit of C++20 concepts?

Pick the cleanest answer.

Common mistakes

Putting template definitions in .cpp files (must be in headers, the compiler needs the full definition to instantiate). Over-templatizing (every fn template = slower builds). Not using concepts in C++20+ code. Excessive SFINAE in C++17 code when a simple overload would do.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Smart Pointers and RAII
Back to C++
Move Semantics→