Next Up Previous Hi Index

Chapter 17

Templates

\author{Paul Bui}

Now that you have a decent amount of experiencing coding, allow me to ask you a question. Have you noticed how many functions that perform the same tasks look similar? For example, if you wrote a function that prints an int, you would have to have the int declared first. This way, the possibility of error in your code is reduced, however, it gets somewhat annoying to have to create different versions of functions just to handle all the different data types you use. Oh wait...we've got templates.

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>

\tt{TYPEPARAMETER} is just the arbitrary typeparameter name that you want to use in your function. Let's say you want to create a swap function that can handle more than one data type...something that looks like this:

template <class SOMETYPE>
void swap (SOMETYPE &x, SOMETYPE &b)
{
  SOMETYPE temp = a;
  a = b;
  b = 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>. For example, if you had "SUPERDUPERTYPE" instead of "SOMETYPE," the code would look something like this:

template <class SUPERDUPERTYPE>
void swap (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 typeparameter, as long as it is not a reserved word.

If you want to have more than one template typeparameter, then the syntax would be:

template <class SOMETYPE1, class SOMETYPE2, ...>


17.2 Templates and Classes

{templates!in classes}

Let's say that rather than creating a puny little templated function, you would rather use templates in a class, so that the class may handle more than one datatype. If you've noticed, pmatrix and pvector are both able to handle creating matrices and vectors of int, double, and etc. This is because there is a line, template <class SOMETYPE> in the line preceding the declaration of the class. Just take a look:

template <class SOMETYPE>
class pmatrix {
  ...
  ...
  ...
};

If you want to declare a function that will return your typeparameter then replace the return type with your typeparameter name.

template <class SOMETYPE>
SOMETYPE printFunction();


17.3 The Pitfalls of Templates

Ok, so now you probably think that templates are the coolest things in the world. There is however, the all too familiar problem of getting the actual code to work. The templated aspects of your function will only work if the type that you are using already has the constructors, operators, and etc. defined. For example, if you were to use the += operator with your typeparamter:

SOMETYPE += x;

However, if the datatype, class, or struct that you use SOMETYPE to represent does not have a += operator defined, then the compiler, specifically, the linker will give you an error and your start losing hair. Don't worry, this is all a part of the sometimes seemingly difficult process of getting templates and the like to work. Have fun.


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 typeparameter.
typeparameter
The typeparameter is the arbitrary label or name that you use in your template to represent the various datatypes, structs, or classes.


Next Up Previous Hi Index