C++ Templates Explained | Function Templates, Class Templates, Specialization, Variadic Templates


This complete tutorial on C++ Templates explains how to write generic and reusable code using function templates, class templates, template specialization, and variadic templates. Following best practices, it helps learners create type-independent, flexible, and efficient C++ programs.

Templates – Complete Tutorial

1. What are Templates?

Templates allow generic programming by enabling functions or classes to work with any data type without rewriting code for each type.

  1. Two main types: Function templates and Class templates
  2. Enables code reuse and type safety

2. Function Templates

Function templates allow a single function to work with different data types.

Syntax:


template <typename T>
T add(T a, T b) {
return a + b;
}

Example:


#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
return a + b;
}

int main() {
cout << add(5, 10) << endl; // int
cout << add(2.5, 3.5) << endl; // double
}

Notes:

  1. typename and class are interchangeable
  2. Compiler deduces the type automatically

3. Class Templates

Class templates allow a single class to handle multiple data types.

Syntax:


template <class T>
class Box {
T length;
public:
Box(T l) : length(l) {}
void show() { cout << length << endl; }
};

Example:


int main() {
Box<int> intBox(10);
Box<double> doubleBox(5.5);
intBox.show();
doubleBox.show();
}

4. Template Specialization

Template specialization allows you to customize behavior for specific data types.

Example:


#include <iostream>
using namespace std;

template <class T>
class Printer {
public:
void print(T value) { cout << "Generic: " << value << endl; }
};

// Specialization for char*
template <>
class Printer<char*> {
public:
void print(char* value) { cout << "String: " << value << endl; }
};

int main() {
Printer<int> p1;
Printer<char*> p2;

p1.print(100);
p2.print("Hello Templates");
}

5. Variadic Templates (Basics)

Variadic templates allow functions or classes to accept any number of arguments.

Example:


#include <iostream>
using namespace std;

// Base case
void print() {}

// Recursive variadic template
template <typename T, typename... Args>
void print(T first, Args... args) {
cout << first << " ";
print(args...);
}

int main() {
print(1, 2.5, "Hello", 'A');
}

Notes:

  1. Introduced in C++11
  2. Useful for flexible argument handling
  3. Can be combined with fold expressions in C++17 for simpler code

Best Practices

  1. Use templates for generic programming
  2. Prefer type-safe code over macros
  3. Specialize templates only when necessary
  4. Keep variadic templates readable and documented
  5. Combine with constexpr and RAII for modern C++

Common Mistakes

  1. Forgetting typename or class keyword
  2. Over-specializing templates unnecessarily
  3. Using templates for very simple cases where functions suffice
  4. Writing recursive variadic templates without base case

Summary

In this chapter, you learned C++ Templates, including:

  1. Function templates for type-independent functions
  2. Class templates for reusable classes
  3. Template specialization for custom behavior
  4. Variadic templates for flexible argument lists

Templates are a powerful feature of C++ that allow generic, reusable, and efficient code, forming the foundation for the STL and modern C++ programming.