Templates

C++ templates are a form of generic programming. They allow us to write functions that work on a variety of data types.

The main advantage is that is saves on development time, since we don’t need to maintain specialized functions for each datatype that we may want to use.

We’ve been using templates all throughout the semester – basically whenever you see the < >. For example, std::vector is a template-class and we tell it what type of vector we wish to create through the type parameter, e.g., std::vector<double>

Here’s a simple example of a function template:

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

Here T stands in for the data type that we will use when calling this function. The compiler will determine which data types are used with add and it will create and compile a version of the function that works with each of the types.

Tip

We can use either typename or class for the template parameters. Only in rare instances is the difference important.

https://stackoverflow.com/questions/2023977/difference-of-keywords-typename-and-class-in-templates

We can call this with double s as:

double x{3}, y{4};
double z = add<double>(x, y);

But in this case, the compiler can infer the type T from the inputs, so we don’t need to include <double>, and can instead simply do:

double z = add(x, y);

Here’s an example illustrating all of this:

Listing 85 simple_templates.cpp
#include <iostream>

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

int main() {

    double x{1}, y{2};

    auto z = add<double>(x, y);
    std::cout << z << std::endl;

    int a{-2}, b{4};

    std::cout << add<int>(a, b) << std::endl;

    std::cout << add(x, z) << std::endl;
}

try it…

Make the function take two potentially different types,

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

Notice that the return type is the same as the first argument.

What happens if you call it with int, double compared to double, int – is it still commutative?