Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message

PostPosted: Sat Oct 28, 2006 8:54 pm   Post subject: [C++]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.


template <typename T>

void swap(T& a, T& b)
        T c = a;
        a = b;
        b = 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:


template <typename T,typename U>


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.


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

bool my_bool = true, other_bool = false;
std::cout << my_bool << other_bool << std::endl;
std::cout << my_bool << other_bool << std::endl;

What do we get from that?

Output wrote:


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.


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

PostPosted: Sat Oct 28, 2006 10:54 pm   Post subject: (No subject)

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

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

Freakman wrote:

template <typename T,U>

This is not correct. I think you mean:

template <typename T, typename U>

How about templates that accept values, rather than typenames?

Default values for template parameters?

Partial template specialization?

PostPosted: Mon Oct 30, 2006 11:18 pm   Post subject: (No subject)

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

PostPosted: Mon Oct 30, 2006 11:49 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to: