Templates

Templates in C++ are a powerful feature that allows you to write generic code. This means you can create functions and classes that work with different data types without rewriting the same code multiple times.

What is a Template?

A template is a blueprint that tells the compiler to generate functions or classes for any data type.

It removes code repetition and improves flexibility.

Why Use Templates?

Templates are useful because they:

  • Reduce code duplication
  • Make code reusable
  • Work with multiple data types
  • Improve code flexibility
  • Are widely used in STL

Types of Templates in C++

There are two main types:

  • Function Templates
  • Class Templates

Function Templates

A function template allows a single function to work with different data types.

Syntax

template <typename T>

Example of Function Template

#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {

return a + b;
}

int main() {

cout << add(10, 20) << endl;
cout << add(5.5, 2.5) << endl;

return 0;
}

Output

30
8

How It Works

  • T is a placeholder for any data type
  • Compiler automatically decides the type
  • Same function works for int, float, double, etc.

Class Templates

A class template allows a class to work with different data types.

Syntax

template <typename T>
class ClassName {
T data;
};

Example of Class Template

#include <iostream>
using namespace std;

template <typename T>
class Box {

public:

T value;

void setValue(T v) {
value = v;
}

T getValue() {
return value;
}
};

int main() {

Box<int> b1;
b1.setValue(100);
cout << b1.getValue() << endl;

Box<string> b2;
b2.setValue("Hello");
cout << b2.getValue() << endl;

return 0;
}

Output

100
Hello

How Class Templates Work

  • One class works for multiple data types
  • Type is defined when creating an object
  • Same logic is reused

Real-Life Example

Think of a template like a form:

  • One form can be used for students, employees, or customers
  • Only the data changes, not the structure

This is how templates work in programming.

Advantages of Templates

  • Reduce repetitive code
  • Improve reusability
  • Support multiple data types
  • Make programs scalable
  • Used heavily in STL

Common Mistakes with Templates

  • Forgetting template declaration
  • Using wrong data types
  • Not understanding type deduction
  • Mixing incompatible operations

Difference Between Function and Class Templates

Function TemplateClass Template
Works with functionsWorks with classes
Used for operationsUsed for data structures
Simpler usageMore complex structure

Applications of Templates

Templates are used in:

  • STL containers (vector, map, list)
  • Generic algorithms
  • Data structure implementation
  • Reusable libraries
  • Large software systems

Why Templates are Important

Templates are important because they:

  • Enable generic programming
  • Reduce code repetition
  • Improve performance and scalability
  • Are a core part of modern C++

Conclusion

Templates in C++ are a powerful feature that allows writing flexible and reusable code. By using function and class templates, developers can create programs that work with multiple data types efficiently without rewriting code.

Home » Professional C++ > Advanced Concepts > Templates