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.
- Two main types: Function templates and Class templates
- 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:
typenameandclassare interchangeable- 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:
- Introduced in C++11
- Useful for flexible argument handling
- Can be combined with fold expressions in C++17 for simpler code
Best Practices
- Use templates for generic programming
- Prefer type-safe code over macros
- Specialize templates only when necessary
- Keep variadic templates readable and documented
- Combine with constexpr and RAII for modern C++
Common Mistakes
- Forgetting
typenameorclasskeyword - Over-specializing templates unnecessarily
- Using templates for very simple cases where functions suffice
- Writing recursive variadic templates without base case
Summary
In this chapter, you learned C++ Templates, including:
- Function templates for type-independent functions
- Class templates for reusable classes
- Template specialization for custom behavior
- 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.