Beginning templates

Templates are a terrific functionality in C++ which is used way too little! A template is a pattern for how a class or function taking a particular type should be created.  Lets say that you need a class which holds a stack of integers, you can simply create this as a regular class. If you later discover that you also need a class which holds a stack of  strings then you are forced to create a new class which does exactly the same thing but just using a different type of data. Creating two classes which does exactly the same thing is really wasting your time as a developer! The solution here is to create a template class; by doing so you create a pattern for how your stack class should work, but you don’t say the exact type that you will be working on.

To make a template class representing a stack, simply add the template keyword when declaring the class:

 

Template function

Just like you can create a template class, it is also possible to make a template function where the type of the inputs is determined at compile time, when the function is being called. Say that you want to create a function to swap two variables, you could either create one function to swap two integers

and then create an almost identical overload taking two doubles, one overload taking two strings, etc. Or you could just create one template function:

To call the template swap you don’t need to explicitly give the type of the variables (as you have to do when instantiating a template class):

Sometimes however, you may want to help the compiler determining the type of the variable. Say that you are calling a function with a literal string where the compiler would generate the template function with the type char* but you would much rather have the template function work on std::string. Then you can either tell the compiler that the type of the template function is std::string, or promote the literal string into a std::string;

 

Facebookgoogle_pluslinkedinmail

Metaprogramming

Metaprogramming in C++ (a.k.a template metaprogramming) uses the fact that templates are instantiated at compile time an not at run time. This makes it possible to create a template which calculates a value at return time, a value which can then be used as a constant in the rest of the program.

Say that we create a template struct which only contains a single value

the definition of the value is recursive and refers to the calculation of the factorial of a value one-less than the requested value. However, this will not in itself compile since the compiler will get stuck in an infinite loop trying to calculate the factorial of one-less than the value eventually crashing the compilation. To stop the compiler it must be given a stopping condition, a special case where we explicitly tell the compiler what the value of the factorial is. This is done like this:

And to calculate the value of the factorial, you simply need to create an instance of the template:

 

See also the C++ book: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Templates/Template_Meta-Programming

Facebookgoogle_pluslinkedinmail

Template Specialization with a constant value

Sometimes you want to create a template based not on a type but using a specific value as input. In languages like C# and Java is this not possible, but in C++ is it allowed.

This can be done like this:

Facebookgoogle_pluslinkedinmail

Using RAII to raise the current thread priority temporarily (Windows specific)

In a recent project I had the need to raise the priority of the currently executing thread for a short period of time while an important I/O operation was being performed (the device which the data was read from didn’t have a buffer to store the data hence the reading thread had to be ready at all times to receive the data). I ended up creating a helper class which uses RAII to manage the priority of the current thread. When the object is created on the local scope the priority of the thread is raised and when the object goes out of scope then the destructor will make sure that the priority of the current thread will go back to its original value.

The class deletes the overload of the operator ‘new’ which makes sure that it is impossible to create the object on the free store.

The code to get the current thread and to change the thread priority is Windows specific but I guess this can quite easily be modified to fit other OS:s as well.

 

 

 

Facebookgoogle_pluslinkedinmail