Templates in C++ and Generic Code in C
Write maximum() once and use it for int, double or your own types. Compare C++ templates with C macros and _Generic.
- Function templates and type parameters
- How the compiler creates each version
- C alternatives: macros, void* and _Generic
maximum<T>written oncemaximum<int>maximum(3, 9) → 9maximum<double>maximum(2.5, 7.0) → 7.0
One pattern, many types
The problem
Writing maximum for int, then for double, then for strings repeats the same code each time. Templates let you write the logic once and have the compiler produce a version for every type you use.
Think of it like this
A template is like a cookie cutter. You design the shape once, and you can press it into chocolate dough, plain dough or gingerbread. Each cookie is a real, separate cookie made from the same pattern.
template<class T>✂️ Design the cutter oncemaximum(3, 9)🍪 Press it into int doughmaximum(2.5, 7.0)🍪 Press it into double dough_Generic📋 C: a list of cutters you made by handA C++ template is a pattern for a function or class with a type parameter, from which the compiler generates a separate, type-checked version for each type used.
Important points
template<class T>makesmaximumwork for any typeTthat supports>.maximum(3, 9)creates an int version andmaximum(2.5, 7.0)a double version.- In C,
_Genericpicksmax_intormax_double, but each one must be written by hand. - Templates are resolved at compile time, so they cost nothing extra when running.
When to use it
- Generic algorithms and containers, like the whole standard library.
- Removing duplicated code that differs only by type.
- Type-safe alternatives to C macros and void pointers.
When not to use it
- Only one or two types: plain overloads can be easier to read.
- Code where long template error messages would slow down a beginner team.
C and C++ code examples
#include <stdio.h>
int max_int(int left, int right) {
return left > right ? left : right;
}
double max_double(double left, double right) {
return left > right ? left : right;
}
#define MAX(left, right) _Generic((left), \
int: max_int, double: max_double)((left), (right))
int main(void) {
printf("%d %.1f\n", MAX(3, 9), MAX(2.5, 7.0));
}
9 7.0
#include <iostream>
template<class T>
T maximum(const T& left, const T& right) {
return left > right ? left : right;
}
int main() {
std::cout << maximum(3, 9) << ' '
<< maximum(2.5, 7.0) << '\n';
}
9 7
C and C++ comparison
The C11 _Generic expression selects a typed function but requires listing supported types. A C++ function template works for every type satisfying its operations.
C uses naming conventions, callbacks, macros, and explicit context structures.
Language features provide scoped, type-safe, and often zero-overhead abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Write the C mechanism before the C++ abstraction.
- Remove manual cleanup with RAII.
- Check whether the abstraction adds allocations or virtual dispatch.
Common mistakes
Mixed argument types
maximum(3, 2.5) cannot deduce one T from int and double.
maximum(3, 2.5);
maximum<double>(3, 2.5);
Using a type that lacks the operation
T must support >, or the generated code fails to compile.
maximum(point_a, point_b); // no > for Point
bool operator>(const Point&, const Point&); // define it first
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Array Maximum Find the largest value in a list. Easy
- Min Stack Build a stack that reports its smallest element instantly after any push or pop. Medium