Templates and Generic Programming – CPP Tutorial

9/18/2025

#Templates Generic Programming – CPP Turial

Go Back
#Templates  Generic Programming – CPP Turial

Templates and Generic Programming – C++ Tutorial

Introduction

In C++, templates enable generic programming, a powerful paradigm that allows you to write code that works with any data type. Rather than creating separate versions of the same function or class for different data types, templates allow developers to reuse the same logic efficiently.

This tutorial explains the concept of templates, their types, syntax, and real examples to help you master generic programming in C++.


What is Generic Programming?

Generic programming is a programming technique that focuses on creating algorithms and data structures independent of specific data types.

Key advantages of generic programming:

  • Code reusability: Write once, use for any data type.

  • Type safety: Errors are caught during compilation.

  • Efficiency: Less code duplication and easier maintenance.

  • Flexibility: Same logic can work with multiple types.


Types of Templates in C++

There are mainly two types of templates in C++:

  1. Function Templates

  2. Class Templates


Function Templates

Function templates let you create a single function that works with different data types.

Syntax

template <typename T>
T functionName(T a, T b) {
    // code logic
}

Example

#include <iostream>
using namespace std;

template <typename T>
T add(T x, T y) {
    return x + y;
}

int main() {
    cout << "Int sum: " << add(5, 10) << endl;
    cout << "Double sum: " << add(3.2, 4.8) << endl;
    return 0;
}

Output

Int sum: 15
Double sum: 8

Class Templates

Class templates allow you to create a single class that can handle any data type.

Syntax

template <class T>
class ClassName {
    T data;
public:
    ClassName(T d) : data(d) {}
    void show() { cout << data << endl; }
};

Example

#include <iostream>
using namespace std;

template <class T>
class Developer {
    T experience;
public:
    Developer(T e) : experience(e) {}
    void showExperience() {
        cout << "Experience: " << experience << endl;
    }
};

int main() {
    Developer<int> dev1(5);
    Developer<string> dev2("Expert");

    dev1.showExperience();
    dev2.showExperience();
    return 0;
}

Output

Experience: 5
Experience: Expert

Template Specialization

Sometimes you need to handle specific data types differently. This is done using template specialization.

Example

#include <iostream>
using namespace std;

template <class T>
class Company {
public:
    void info(T data) {
        cout << "General data: " << data << endl;
    }
};

// Specialization for string
template <>
class Company<string> {
public:
    void info(string data) {
        cout << "Company Name: " << data << endl;
    }
};

int main() {
    Company<int> c1;
    Company<string> c2;

    c1.info(500);
    c2.info("TechCorp");
    return 0;
}

Output

General data: 500
Company Name: TechCorp

Best Practices

  • Use clear template parameter names like T, U, Type.

  • Keep template definitions in header files for reusability.

  • Avoid overly complex nested templates.

  • Use typename instead of class for better clarity.


Conclusion

Templates and generic programming are core features of modern C++ that help you write type-safe, reusable, and efficient code. By mastering function templates, class templates, and specialization, you can build flexible components that work with any data type.


Meta Description

"Learn templates and generic programming in C++ with real examples. Understand function templates, class templates, and template specialization in this beginner-friendly tutorial."

Meta Keywords

templates in C++, generic programming in C++, function templates, class templates, template specialization, C++ tutorial templates