Modern C++ and C Alternatives

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.

  • Advanced
  • 3 min read
  • 2 runnable examples
What you'll learn
  • Function templates and type parameters
  • How the compiler creates each version
  • C alternatives: macros, void* and _Generic
template <class T> T maximum(T, T)
  • maximum<T>written once
    • maximum<int>maximum(3, 9) → 9
    • maximum<double>maximum(2.5, 7.0) → 7.0
Each use with a new type stamps out a new version at compile time.

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.

From a cookie cutter to code
template<class T>✂️ Design the cutter once
maximum(3, 9)🍪 Press it into int dough
maximum(2.5, 7.0)🍪 Press it into double dough
_Generic📋 C: a list of cutters you made by hand
One pattern; the compiler generates a version per type.
In one sentence

A 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

  1. template<class T> makes maximum work for any type T that supports >.
  2. maximum(3, 9) creates an int version and maximum(2.5, 7.0) a double version.
  3. In C, _Generic picks max_int or max_double, but each one must be written by hand.
  4. 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

C
Run code →
main.c
#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));
}
Get a Run button for this example Result of running the code
9 7.0
C++
Run code →
main.cpp
#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';
}
Get a Run button for this example Result of running the code
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

C uses naming conventions, callbacks, macros, and explicit context structures.

C++

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.

Problem
maximum(3, 2.5);
Fix
maximum<double>(3, 2.5);

Using a type that lacks the operation

T must support >, or the generated code fails to compile.

Problem
maximum(point_a, point_b);  // no > for Point
Fix
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.

All challenges