understanding templets

I have been programming for about 3 years now (C++), and i have been well able to make programs that solve my problems without ever using a template. I am going to start reading-up, but i just thought i'd ask....why would one need a template??? Could some one give a very simple answer to this?
Last edited on
Templates let you write code that is "blind" to the type of a variable. This means you can write code that will work with any type of variable instead of just one type.

For example, let's say you want to write a simple function to triple something:

1
2
3
4
int triple(int v)
{
  return v;
}


Seems simple... but it only works with ints. What happens when you try to do this:

1
2
double foo = triple(1.6);
cout << foo;  // surprise!  prints 1! 


The problem is that triple takes an int, and therefore when you give it doubles, precision is lost.

One solution to this is to make triple a template:

1
2
3
4
5
template <typename T>
T triple(T v)
{
  return v * 3;
}


As a template, the compiler will just replace the 'T' with whatever type you give it... so now triple will work with any type that can be multiplied by 3. float, char, int, double... it doesn't matter. It can even work with classes you create that have the * operator overloaded.



template classes go even further and allow you to create entire classes based around a variable type. This is how the container classes like vector, list, map, etc work.
Thanks Disch! but it is a couple of things that I dont understand..."T triple(T v)" (line 2 of your last block) does not triple only have one argument???? And is template T a class with triple as a member function something like" T.triple(T,v)?????????
is template T a class with triple as a member function

This line:

template <typename T>

is a message to the compiler, telling it the next function is a template function where "T" is a "place-holder type", for lack of a better term.

this definition (call it definition 'A'):

1
2
3
4
5
template <typename T>
T triple(T v)
{
  return v * 3;
}

is a place-holder for functions that will be generated at compile time.

Say, that your code contains the following calls to "triple" somewhere in the code:

1
2
3
4
5
int tripleInt = triple(5);
...
float tripleFloat = triple(6.2);
...
double tripleDouble = triple(3.2);

then the compiler will expand 'A' to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int triple(int v)
{
  return v * 3;
}

float triple(float v)
{
  return v * 3;
}

double triple(double v)
{
  return v * 3;
}

If you only call int tripleInt = triple(5); in your code, then only the int version of the function would be generated.

Of course, this generation happens automatically. It's easier to think of "triple", as Disch said, as a function that could take any type as a parameter (so long as you can multiply it by 3).

does not triple only have one argument

Yes, it has one argument ("v" of type "T").
Thanks shacktar this is really clearing things up for me!!!!!! So i have to ask...why dont we make every function a template so we dont run into datatype problems????? Is it bad practice????
Last edited on
Because it doesn't make sense to do that. Some functions will only make sense to work with a specific type.

If you try writing a program where you make every function a template, you'll see very quickly why it doesn't work.
Making every function a template function is not good solution, because often times it would just make things more complicated and difficult to maintain. One good example would be dealing with header & source files. Because templates are compiled in runtime, linkage between header & source file is not possible, so if you happen to have a template function you have to declare & define it in the same file (header or source file). However, I´ve heard that export template functions would increase modularity (linkage between header(s) & source file(s)), but I don´t know if this is true or not. Oh, and in addition, export -keyword is not supported by many modern compilers anyway.

If you are interested to know more about export template functions, check this out:
http://warp.povusers.org/programming/export_templates.html
Last edited on
Topic archived. No new replies allowed.