Demystifying C++ Templates for Beginner Programmers

Demystifying C++ Templates

Today we take a break. After the sixth part in our tutorial series on the C++ Standard Template Library, we briefly talk about what C++ templates are. For the past few months, we have been discussing the STL. At the heart of the STL is the template. In spite of that, we have not really made a beginner introduction to C++ templates.

Just in case you are already familiar with templates in C++, you can skip this tutorial and go straight to and start the tutorial on the Standard Template Library series.

A Case for C++ Templates

In computing, a template can be described as a preset format for a function, document or file, used so that the format does not have to be recreated each time it is used. In the C++ language, this definition holds true. To give a better illustration we will be using some code examples, therefore, we do make assumptions that you are already familiar with how to write functions in C++.

If you are not familiar with C++ functions then may need to take the modern C++ tutorial series we will be publishing here on Sobbayi Softworks. For the longest time, one of the strengths of C++ has been its ability to allow the programmer to program in templates and the containers found in the C++ Standard Template Library are a good example of the power of C++ templates. The code is very stable fast and efficient and comes highly recommended for use wherever it can be used. We will not be talking too much about STL here as there is an entire series on that.

Read Also  Qt 5.12.9 LTS Release With Bug Fixes is now Available

Just as a point to note. There are other third party libraries out there such as Boost that make great use of templates and I would also advise you to check them out here.

Assuming we have a function that does something trivial like simple arithmetic, The following code would satisfy our needs perfectly:

The code above seems quite fine. Its intention is to square and integer and return the result. In computing nothing ever remains the same. sooner or later for various reasons, the programmer might want to extend this program to be capable of squaring not just integers but doubles as well. Simple enough. The following code would get the job done just as well.

Well, that is not too bad. However, from a maintenance point of view especially for programmers who intend to write libraries, you will find this situation becomes a headache when you need to cater for more types. The simple problem is that you would have to write a function for each type and that eats into the finesse of maintaining such code.

Templates to the Rescue

To solve the issue above C++ comes with a language feature that allows you to create a template to address the redundancies illustrated above. Let us rewrite the code above this time using a template.

Read Also  How to Disable Multiple WordPress Post Revisions

Notice the code above returns the same result as the previous code snippet. Furthermore, it will still work with other types such as float and other complex types. So what exactly is happening here?

In C++ you define a template by prepending template <typename T>  before a function or a class. Where you would define the type in question in the definition you would replace it with the defined type which in this case is “T”. “T” could be anything. The convention here is to simplify it as “T”. During compilation, the compiler will replace every occurrence of “T” with the predefined type in the angle brackets wherever the function or class is called. Such as the example in the code above squareThis<int>(2); .

Note: With template function, the type can be inferred from the parameter passed to the function. So typing squareThis(2);  would still work just as well. For purposes of code maintainability, it might be prudent to use the angle bracket to keep the code clean. As for class templates, you need to specify the type in angle brackets.

The other thing to note is the compiler creates new functions from the templates in memory with the actual types used in the code. In our example, the compiler will replace our template with two functions. One for int and another for the double.

Read Also  How to use Screen Size to Execute JavaScript

Here is an example showing a class template.

That is it for now. With this, I hope the usage of containers in the STL tutorial series will no longer look strange. We will be discussing more about templates in the upcoming C++ programming series that begins in April. In that series, we will be teaching you how to program in modern C++ (C++11, C++14 and C++17).

Meanwhile, we continue with the seventh part of the C++ Standard Template Library this time we are looking at non-modifying algorithms.