Explain function templates in C++, and provide an example.
You may have come across a time when you created multiple functions to solve the same exact problem in C++ – and that may have seemed redundant to you at the time. And why did you create multiple functions? Well, because you needed separate functions to handle a different type – one function to handle an int, another to handle a char, and yet another to handle floats. So, even though each function had the same exact algorithm, you had to create multiple functions just so you could handle all the different types. But that sounds awfully inefficient, right? Wouldn’t it be easier to just have one function that handles all the different types – and somehow have C++ “plug in” the types? Well, that’s what function templates are all about.
When do we need a function template in C++?
And, to make it very clear why we would need function templates, let’s start with a small example. Suppose we have the following function that is used to swap 2 variables – in this case 2 variables of type float:
void swapVariables(float& var1, float& var2) { float temp; temp = var1; var1 = var2; var2 = temp; }
But, what if we wanted to have a function swap 2 variables of type int, and not just of type float? Well, we would then have to overload this function by changing the parameters to type int, along with changing the type of the “temp” variable. Here’s what it would look like:
void swapVariables(int& var1, int& var2) { int temp; temp = var1; var1 = var2; var2 = temp; }
Now, what if we want to create another function that would be able to swap 2 values of type char? Well, then we would have to overload the function again to make it look like this:
void swapVariables(char& var1, char& var2) { char temp; temp = var1; var1 = var2; var2 = temp; }
So, you can see now that we’ve created 3 functions that do exactly the same thing, but just use different types. Isn’t there a better way to do this – so that all we have to do is just create one function that accepts all the different types? Yes, there is! This way you don’t have to have messy code with a bunch of functions that essentially run the same algorithm – which in our example is swapping 2 variables And, that is exactly why we have function templates – so we don’t have to replicate code to account for different types! And now, here is what the definition of a function template would look like for a function that would swap 2 variables. We’ll explain this code in further detail as well:
Example of a function template in C++:
// the template prefix: template<class T> void swapVariables(T& var1, T& var2) { T temp; temp = var1; var1 = var2; var2 = temp; }
That is what the function template would look like for the swapping function that we were discussing earlier. Now, let’s analyze it. The line that says “template” is called the template prefix, and it basically tells the compiler that the function definition underneath is a template, and “T” is called a type parameter.
One thing that is confusing, and important to understand: in this context, the word “class” means the same thing as type. So, the way to think of “class T” is that T is a type, but it can be any type – whether a float, int, double, etc. – whatever type is passed into the function is what “T” becomes. You can also see in the code above that the declaration of the temp variable looks like this: “T temp”. Remember that T is the type that is passed in when the function is called – we do not know what that type is until the function is called – it could be an int, a float, etc. So, the “temp” variable will be whatever type is passed when the function is called.
This leads us to the next question: How do we actually call this function and tell it what type to use? Well, this is what the code for that would look like:
How to call a function template:
// the template prefix: template<class T> void swapVariables(T& var1, T& var2) { T temp; temp = var1; var1 = var2; var2 = temp; } int main( ) { float float1 = 3.5, float2 = 5.6; //call to the function: swapVariables(float1, float2); } //
You can see in the main function that we make a call to the swapVariables( ) function with this call: “swapVariables(float1, float2);”. So, calling a function template is exactly like calling a normal function! The compiler will figure out the type of the variable(s) being passed into the function template, and then the “T” type will be set to the type of the variable(s) passed in – which in our example above is float.