C++ Templates

Sometimes you write a function that performs the same task for different data types. For example, you may want to find the larger of two int values and later do the same thing with double values.

Without templates, you might need to write separate functions for each data type. C++ Templates allow you to write one general piece of code that can work with different data types.

Templates are mainly used with functions and classes.

Function Templates in C++

A function template allows you to create one function that can work with different data types.

Example

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

Here, T is a placeholder for a data type.

You can use the same function with integers:

cout &lt;&lt; maximum(10, 20);

Output

20

You can also use it with decimal values:

cout << maximum(10.5, 7.2);

Output

10.5

C++ determines the appropriate type from the values passed to the function.

typename is used to tell C++ that T represents a type. You may also see class used in its place:

template <class T>
T maximum(T a, T b)
{
    return (a > b) ? a : b;
}

Both forms work for this purpose.

Another Function Template Example

Let’s create a function that displays a value:

template <typename T>
void display(T value)
{
    cout << value << endl;
}

Now the same function can be used with different types:

display(100);
display(25.5);
display("Hello");

Output

100
25.5
Hello

This is the main advantage of Function Templates in C++. You can avoid writing almost identical functions for different data types.

Class Templates in C++

Templates can also be used with classes. A class template allows you to create a class that can work with different data types.

Example

template <typename T>
class Box
{
private:
    T value;

public:
    Box(T v)
    {
        value = v;
    }

    void display()
    {
        cout << value;
    }
};

Now you can create objects using different data types:

Box<int> b1(100);
Box<double> b2(25.5);

b1.display();
b2.display();

Output

10025.5

Here, T becomes int for b1 and double for b2.

You can also use a string:

Box<string> b3("Hello");

A Class Template in C++ is useful when the structure and functions of a class remain the same, but the type of data it stores may change.

Template Specialization in C++

Usually, a template provides the same general logic for different types. But sometimes one particular type needs different treatment. Template Specialization in C++ allows you to provide a separate implementation for that specific type.

For example:

template <typename T>
void display(T value)
{
    cout << "Value: " << value;
}

Suppose you want string values to be displayed differently. You can specialize the template:

template <>
void display<string>(string value)
{
    cout << "Text: " << value;
}

Now:

display(10);
display("Hello");

The general template is used for the integer, while the specialized version is used for the string.

Template specialization is useful when the general template works for most types but one particular type requires different behavior.


Frequently Asked Questions (FAQs)

Q1. Why are templates used in C++?

Templates allow you to write reusable code that works with different data types without creating separate versions of the same function or class.

Q2. When should I use a function template in C++?

Use Function Templates in C++ when the same operation needs to work with different data types.

Q3. How are class templates different from function templates?

Class Templates in C++ create generic classes that can work with different data types, while function templates make individual functions reusable across types.

Q4. Do C++ templates improve code reusability?

Yes. C++ Templates reduce duplicate code by allowing the same logic to work with multiple data types.

Q5. What is template specialization used for?

Template Specialization in C++ allows you to provide a different implementation for a specific data type when the general template does not suit that type.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top