I've been reading up on templates and they just seem like so much hastle for so little reward. I can't find anyway to effectively use it over vectors. I am just having a lot of trouble learning them because the concept seems so weird.

Recommended Answers

All 7 Replies

just a few templates you have probably already used
vector
string
fstream
ifstream
ofstream

commented: What the fuck are you talking about? -1

I've been reading up on templates and they just seem like so much hastle for so little reward. I can't find anyway to effectively use it over vectors. I am just having a lot of trouble learning them because the concept seems so weird.

Google "C++ templates tutorial"
Lots of useful links, for example:
http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm

I've been reading up on templates and they just seem like so much hastle for so little reward. I can't find anyway to effectively use it over vectors.

The standard vector is a template. A std::vector<int> is a different thing from a std::vector<double>, but both are implemented (within the library that comes with your compiler) using the same code.

I am just having a lot of trouble learning them because the concept seems so weird.

There are lots of scenarios. I'll just give you one. It is real, although I've described it in a hand-wavy manner. You'll have to think through it and understand in order to be convinced.

Picture you've designed a class or an algorithm (let's call it X) that works with data of type int.

Late on, you think to yourself "X only works with integers, but it could work with data of other types".

Without templates, you might copy the code from X and create a new version X1 that does the same thing with the double type, and a new version X2 that does the same thing with a string type, and X3 that works with some other type of data. To do that, you effectively have to copy the same code three times, with the only real difference between X1, X2, and X3 being their names and the type of data they act on.

After you've done that, you find and fix a bug in X, or add a feature to X. Now you have to go back and fix the same bug in X1, X2, and X3. So your effort to maintain your code has just been multiplied by 4.

Templates give you a means to avoid the duplication of code - albeit by making the compiler work harder. By simply prefixing your declarations with template<class T> and using T instead of int in your code for implementing X, you can have one body of code that works for T being int, double, string, or some other type. Instead of X, X1, X2, and X3 you now have X<int>, X<double>, X<string>, and X<some_other_type>. No, to maintain your code, you just modify the template code .... and all changes automatically get applied by the compiler to X<int>, X<double>, X<string>, and X<some_other_type>.

Function templates allow the programmer to write a single function that can handle differing parameters. A simple example would be if you wish to have a function that swaps two parameters you would need one function for every parameter type, integer, real, char etc.
With a function template you write a single function. The complier recognises that you have passed two integers or two chars and will substitute the correct keyword for the generic type <T>. Class templates work in a similar way. Containers and iterators are common examples. You may want to iterate generic lists, so the list could be integers or chars etc. With a Class Template you only have to write one Class that can handle any parameter type at runtime. Standard Template Library is a rich collection of containers and iterators. Understanding these important components and how they work can save you time that would otherwise be spent on creating your own.

Added to that ,my doubt is if we created a general template lets say for swap two value.In the main of that we created objects of in t type,float type.Will compiler treat both in compiler side implementation?

Added to that ,my doubt is if we created a general template lets say for swap two value.In the main of that we created objects of in t type,float type.Will compiler treat both in compiler side implementation?

The answer to your question is yet unless you tell it to do something else!

This leads onto the other beautiful aspect of templates. You can specialize them.

Let me take an example:

Consider a polynomial class. You would have a template parameter the number of different variables (e.g. with x,y,z etc) and a template parameter for the type(double,complex,polynomial). So you define a solve method (say using Bezout matrix reduction) that works for any number of variables except one, BUT you specialize the solve for just one variable (e.g. polynomial of x, if numeric use GSL, if non-numeric expand and simplify). Then you can use the reduction in a simple recursion loop to solve your polynomial system without extra work. Effectively it doesn't matter how many polynomial variables you have.

It is easier to debug, since if it works for 1 and 2 variables it works for any. You write one class and find that it works for both double precision numbers AND algebraic constructs.

It is much easier to test a set of polynomials of double, than a set of polynomials of Legendre polynomials BUT you can rely on you code because it is templated.

Finally, when you solve something in a function language, the natural method to move it to C++ is via the template forms. Without them I have absolutely no idea how to prototype and code.

The trouble with templates is simply the learning curve. The syntax is fine, you can grasp that in a day or so. It is moving from a procedural outlook to a more functional outlook. I am certain that I didn't understand or see large benefits to templates in C++ until I started programming Haskell.

Having said that, I am sure that some other aspects of templates are non-functional, and I haven't fully groked the possible exploitation of those techniques.

Templates make Template metaprogramming possible; you might want to take a look at it, pretty interesting stuff.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.