I've noticed that there is no tutorial on arguably C++'s most usefull feature, templates. I thought,
what the hell, I might as well write one.
Templates are a way of making functions and classes type independant. They allow you to write
one general function/class, and lets the compiler determine the specifics, namly the type.
Let's take a theoretical example of when templates can be useful. Adding. Say I need to add two
numbers. The process is always the same, x + y. But you have to define different functions to do the
operation with int and doubles. For instance, say I had a program that needed to add two integers
and two doubles at any given time. There are several solutions. One would be to create two
different functions like so:
double addDoubles(double x, double y){ return x+y; }
int addInts(int x, int y) { return x + y; }
Alternativly, if we could make things more professional using function overloading:
double add(double x, double y){ return x+y; }
int add(int x, int y) { return x + y; }
Here the compiler determines at compile-time, which function will be called. Although this is a
better example, you're still writing the same function two times. This isn't a problem with add, but
with a larger function, it can be tedious. This is where templates come into play. Let me throw the
syntax out there:
template <class T>
T add(T x, T y) { return x + y; }
Here we've created a function that is generic. template is a keyword that specifies that this function
is generic. <class T> is the key here. Think of T as a variable that will hold a type. It's a place holder
for a type. If template is called with integer values, T is replaced with int, if it's called with doubles,
T is replaced by double. In this way, add will work with amy base type. Notice that only one
copy of the function is writen. You can use this function just like any other function:
double resultD = add(3.34, 356.54); //calls double version of add
int resultI = add(2, 45); //calls int version of add
In essence, templates allow the compiler to do all overloading, not you. It makes things a lot easier,
and allows you to write functions that are generic and that will work with any type. Handy, eh?
If I wanted to add another function to add floats, I would have to create yet another overloaded
function called add that takes in float. Then I have 3 functions that are the same! With templates, I
wouldn't need to add anything. I could freely just go: float resultF = add(3.2f, 453.3f);
Another benifit of using templates in C++.
Ok, that's a general overview of template functions. Now i'll go over template classes. Let's use a
Stack class as an example. We want to develop a class that will hold a stack of information. Note,
that a stack will hold information the exact same way for every data type. As in every Stack's pop()
and push() method act the same! Now we could make every method of Stack a template, or the
better solution, make the class a template. Here is an example:
template <class T>
class Stack{
int topOfStack;
T info[100]; //Note, it is of type T, which is a place holder for a type
public:
T push(T value) {
info[topOfStack] = value;
topOfStack++;
}
T pop() {
topOfStack--;
return info[topOfStack];
}
};
Now we can create code like this:
Stack<int> intStack;
Stack<doube> doubleStack;
Notice how I need to specify the type in angle brackets when I define the object. For simplicity,
Stack is very limited and not well coded(no bounds checking), but you get the point. I can now call
the methods of both stacks like so:
intStack.push(34);
doubleStack.push(3.1415);
Again, very handy. One final note: I can replace <class T> with <typename T> to achive the same
results. They are the same in thier usage.
Let me know what you thought of this tutorial.
Cinjection.