An introduction to Templates

The template is one of the most powerful tool to have in your toolbox as a C++ developer.

I feel like C++ templates are often misunderstood, misused or overused(when a non-template approach would be just as useful and maybe easier to read and understand). Also, they are often avoided, due to lack of understanding.

There are many C++ programmers that are against templates because of the above mentioned reasons, but maybe, after this simple introduction, templates will be understood better and there will be less people avoiding them.

What’s the use of templates?

This question is pretty simple to answer. You can write code once and instead of providing values as “parameters”, you are going to provide data types. This way, you don’t need to add duplicated code, just because you need one function/class or other C++ entity for each data type but with the same implementation.

For example, instead of having a sum function for multiple data types(one for int, one for float, another for double etc) you can just have a template function sum. For simplicity, we can just say that, the compiler will generate the functions mentioned above for us when using templates.

Generating the functions is a little bit more complicated than that, because these can be generated from implicit or explicit instantiations of the template.

// Without templates
int sum(int a, int b)
{
    return a + b;
}
float sum(float a, float b)
{
    return a + b;
}
double sum(double a, double b)
{
    return a + b;
}
// With templates
template<class T>
T sum(T a, T b)
{
    return a + b;
}

Function and class templates

First of all, there are more types of templates, aside from function and class templates but, since this is an introduction, we will focus on these two.

Just for general information, the other template types are: alias templates, variable templates and concepts and constraints. Maybe we will get more in depth in future articles, if this is well received.

Anyway, let’s start with the function templates. We already have an example above, the one with the sum.

The basic and most used syntax for a function template is:

template<parameter-list>function-declaration

I’ve already provided an example above, but for the clarity, it’s the one with sum function above.

One thing to mention here, is about the implicit instantiation which works for all types of templates and is the most used.

For the previous example:

template<class T>
T sum(T a, T b)
{
    return a + b;
}

We can use the cppinsights tool in order to see what the compiler does for us.

If we just have this definition, the compiler does not generate anything more for us.

But, if we use the actual function with different types of data, the the implicit instantiation takes place. Basically, the compiler generates function signatures for us with all the data types that were used in the call.

You can find a full example of this, here: https://cppinsights.io/s/67d2f758.

For class templates, the most used syntax is:

template<parameter-list>
class-declaration

Here is an example of what can you do with class templates:

// Without templates
class Container
{
public:
    Container(int var) : var_(var) {}
private:
    int var_;
};

class Container1
{
public:
    Container1(float var) : var_(var) {}
private:
    float var_;
};

class Container2
{
public:
    Container2(double var) : var_(var) {}
private:
    double var_;
};
// With templates
template<class T>
class Container
{
public:
    Container(T var) : var_(var) {}
private:
    T var_;
}

The same concept of implicit instantiation of functions works for all other types of templates, including class templates.

And then after use, we will get implicit instantiation.

You can find a full example on cppinsights here: https://cppinsights.io/s/74e61ef7.

In conclusion

I feel like templates are a powerful tool to have in your toolbox as a C++ developer.

This is just an introduction on what templates are, how they work and how you can get started with them. Didn’t want to get into more detailed stuff in this article but if it’s well received I will create a more in depth one in the future.

I hope, you have a better understanding of how you can use templates and you will give them a chance next time.

1 thought on “An introduction to Templates”

  1. Pingback: Templates in C++ – A detailed overview – The CPPDEV

Leave a Reply

%d bloggers like this: