Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Introduction to Templates in C++

What are Templates?

Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This means you can write a function or a class once and use it for any data type. Templates are a powerful tool for creating reusable and flexible code.

Why Use Templates?

Templates are used to create code that is independent of any particular type. This promotes code reusability and can significantly reduce the amount of code you need to write. Templates are particularly useful when you want to apply the same logic to different types of data.

Function Templates

A function template works with any data type. The following example shows a simple function template that returns the maximum of two values.

template <typename T>
T getMax(T a, T b) {
    return (a > b) ? a : b;
}

In this example, <typename T> tells the compiler that T is a placeholder for a data type. The function getMax can now be used with different types, such as integers and floats.

Using Function Templates

To use a function template, simply call the function with the required type.

int main() {
    int a = 5, b = 10;
    double x = 5.5, y = 10.5;

    cout << "Max of a and b: " << getMax(a, b) << endl; // Outputs 10
    cout << "Max of x and y: " << getMax(x, y) << endl; // Outputs 10.5

    return 0;
}
Max of a and b: 10
Max of x and y: 10.5

Class Templates

Class templates are used to create classes that can handle any data type. The following example demonstrates a simple class template for a pair of values.

template <typename T>
class Pair {
private:
    T first, second;
public:
    Pair(T a, T b) : first(a), second(b) {}
    T getFirst() { return first; }
    T getSecond() { return second; }
};

Here, <typename T> allows the Pair class to store two values of any type.

Using Class Templates

To use a class template, specify the type when creating an object of the class.

int main() {
    Pair<int> intPair(1, 2);
    Pair<double> doublePair(3.5, 4.5);

    cout << "First of intPair: " << intPair.getFirst() << endl; // Outputs 1
    cout << "Second of doublePair: " << doublePair.getSecond() << endl; // Outputs 4.5

    return 0;
}
First of intPair: 1
Second of doublePair: 4.5

Template Specialization

Template specialization allows you to define different implementations of a template for specific types. This can be useful when you need a specialized behavior for a particular type.

template <typename T>
class MyClass {
public:
    void display() {
        cout << "Generic template" << endl;
    }
};

template <>
class MyClass<int> {
public:
    void display() {
        cout << "Specialized template for int" << endl;
    }
};

In this example, MyClass<int> is a specialized version of the template for the int type.

Summary

Templates are a powerful feature in C++ that allow you to write generic and reusable code. By using function templates and class templates, you can create functions and classes that work with any data type. Template specialization allows you to provide specific implementations for particular types.

Understanding templates is crucial for mastering C++ and writing efficient, maintainable code.