Computer Science Canada

[C++]Templates

Author:  Clayton [ Sat Oct 28, 2006 8:54 pm ]
Post subject:  [C++]Templates

Templates


What is a Template?

We have all, at some point or another, realized that some functions can do well with more than just one type (ie. adding two integers, or adding two floats/doubles, or adding (concatenating) two strings together) and for each time you wanted to do this, you would have to create a new seperate function for that type. Thankfully in C++, you have the means of ridding yourself of statically typing your functions. The means to do this is templates.

What does a template look like?

A template is really a very simple tool to use, and you will be glad that you found out about them at some point or another. Here I have a basic swap function, which takes two parameters and swaps them. Doing it the old way (ie. statically typed) i would have to create seperate functions for seperate types. Using templates however, I simply have to create one, and then at compile time, a different function is created for each type.

c++:

template <typename T>

void swap(T& a, T& b)
{
        T c = a;
        a = b;
        b = c;
}


c++:

template <typename T>


Because we are creating some new and innovative type in our program, we need to give it a name, the above simply creates a new template with the typename being T (as opposed to something like int or bool). At compile time, in one instance of a function, only one typename can be assigned to a type (ie in swap, it can only swap two ints or bools). If you wish to have more than one non-statically typed function, you must create another typename:

c++:

template <typename T,typename U>


Simple.

Some of you are probably asking what the T& part does. T is the type of the parameter being passed, and the ampersand (&) denotes a referance, meaning its value can change within the function, instead of a simple copy being used. This is much like passing a value in var format in Turing.

How do we call such a function? Easy. Simply call it with two parameters of the same type.

c++:

int my_int = 5, other_int = 7;
std::cout << my_int << other_int << std::endl;
swap(my_int,other_int);
std::cout <<  my_int << other_int << std::endl;

bool my_bool = true, other_bool = false;
std::cout << my_bool << other_bool << std::endl;
swap(my_bool,other_bool);
std::cout << my_bool << other_bool << std::endl;
//etc...


What do we get from that?

Output wrote:

57
75
truefalse
falsetrue


So as you can see, templates are very useful in C++. This was a trivial use of them, and can be expanded further to other things.

Conclusion

And that is that, if you have anything to add, please let me know!

Author:  md [ Sat Oct 28, 2006 10:54 pm ]
Post subject: 

Great post, though something about classes would be good. Classes and templates are a little more tricky Razz

Author:  wtd [ Mon Oct 30, 2006 11:57 am ]
Post subject:  Re: [C++]Templates

Freakman wrote:
c++:

template <typename T,U>


This is not correct. I think you mean:

code:
template <typename T, typename U>


How about templates that accept values, rather than typenames?

Default values for template parameters?

Partial template specialization?

Author:  Null [ Mon Oct 30, 2006 11:18 pm ]
Post subject: 

Admirable effort (and I don't claim to be able to do better), but there are much more comprehensive tutorials online.

Author:  Naveg [ Mon Oct 30, 2006 11:49 pm ]
Post subject: 

I think this is as much a learning experience for the poster as it is for anyone reading. Sure, there are other tutorials online, but teaching is actually one of the greatest ways to become more confident with the material yourself.


: