Next Up Previous Hi Index

Chapter 17

Templates

(original text by Paul Bui)

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 repeatin 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.


17.1 Syntax for templates

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>
void swapvars (SOMETYPE &x, SOMETYPE &y)
{
  SOMETYPE temp = x;
  x = y;
  y = temp;
}

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>
void swapvars (SUPERDUPERTYPE &x, SUPERDUPERTYPE &y)
{
  SUPERDUPERTYPE temp = x;
  x = y;
  y = temp;
}

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, ...>


17.2 Templates and classes

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>
class vector {
  ...
  ...
  ...
};

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>
SOMETYPE printFunction();


17.3 The Pitfalls of Templates

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;
...
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.


17.4 Glossary

templates
Also known as parameterized types, templates allow the programmer to save time and space in source code by simplifying code through overloading functions with an arbitrary type-parameter.
type-parameter
The type-parameter is the arbitrary label or name that you use in your template to represent the various datatypes, structures, or classes.

Revised 2008-12-08.


Next Up Previous Hi Index