Have you noticed how many functions that perform the same tasks look similar? For example, if you wrote a function that swaps two int variables, you would have to have to declare an int temporary variable first. Swapping two double variables follows the same pattern, but you have to declare a double temporary. The possibility of error in your code is reduced by repeating the same pattern, but it gets somewhat annoying to have to create different versions of functions just to handle all the different data types you use. Fortunately, C++ provides templates to automate this process.
Parameterized types, better known as templates, allow the programmer (you) to create one function that can handle many different types. Instead of having to take into account every data type, you have one arbitrary parameter name that the compiler then replaces with the different data types that you wish the function to use, manipulate, etc.
Templates are pretty easy to use, just look at the syntax:template <class TYPEPARAMTER>
TYPEPARAMETER is just the arbitrary type-parameter name that you want to use in your function. Put this template specification in front of any definition you want to parameterize. Let's say you want to create a swap function that can handle more than one data type; you can do it this way:template <class SOMETYPE>
The function you see above looks really similar to any other swap function, with the differences being the template <class SOMETYPE> line before the function definition and the instances of SOMETYPE in the code. Everywhere you would normally need to have the name or class of the datatype that you're using, you now replace with the arbitrary name that you used in the template <class SOMETYPE> specification. For example, if you had "SUPERDUPERTYPE" instead of "SOMETYPE," the code would look something like this:template <class SUPERDUPERTYPE>
As you can see, you can use whatever label you wish for the template type-parameter, as long as it is not a reserved word.
If you want to have more than one template type-parameter, then the syntax would be:template <class SOMETYPE1, class SOMETYPE2, ...>
Let's say that, rather than creating a simple templated function, you would rather use templates in a class, so that the class may handle more than one datatype. If you've noticed, vector is able to handle creating vectors of int, double, string, etc. This is because there is a line, template <class SOMETYPE> in the line preceding the declaration of the vector class. It looks something like this:template <class SOMETYPE>
If you want to declare a function that will return your type-parameter then specify the return type to be your type-parameter name:template <class SOMETYPE>
Templates are very convenient, but there is one pitfall you should avoid. The templated aspects of your function will only work if the type that you are using has already defined the constructors, operators, etc. that your function uses. For example, suppose you were to use the += operator with your type-parameter:SOMETYPE x, y;
If the datatype, class, or structure that you use SOMETYPE to represent does not have a += operator defined, then the compiler (specifically, the linker) will give you an error, because it doesn't know how to do += on that type of variable. You may have to rewrite your templated function so that it uses only the operators and functions defined for all the types of objects that make sense to use with the function. For example, instead of x += y you could write x = x + y if all the relevant types have a + operation.