You might find this a bit silly, but could someone please explain to me how to do C++ templates? I know they're very simple ... I just never learned them nor ever had a teacher who taught them or even made mention of them.

Ahh. Too much to write. It's better if I make a tutorial out of this.

So are you gonna? I need to put the C++ tutorials I wrote on this site.

I'm looking for a CD where I have a bunch of tutorials I wrote a long time ago. I'll put them up when I find it.

Any luck finding them?

I never looked. I think it's in an old box in the garage. I still have to look. 8)

You might find this a bit silly, but could someone please explain to me how to do C++ templates? I know they're very simple ... I just never learned them nor ever had a teacher who taught them or even made mention of them.

This is an old thread but there's hardly any traffic here. Did you find what you wanted?

Nope :( I'm still clueless. I haven't had a need for them so it sorta wandered to the back of my mind. But it would still be great if someone could just fill me in a bit.

You could fill a whole book on C++ Templates - indeed some people already have - as it's a complex subject. Let me simply provide an example and a highly simplified description of what templates are about. If you need more, please ask, but remember that it's a big topic and forum posts will barely scratch the surface.

If we want to write a function that returns the highest of two integers we could easily do so like this:

int maxInt(int x, int y)
    return x < y ? y &#58; x;

It's easy to understand such a function; x is compared with y and if x is lower than y we return y. Otherwise we return x.

If we want to write a function that returns the highest of two floating point values it's just as easy:

double maxDouble&#40;double a, double b&#41;
    return a < b ? b &#58; a;

I'm sure you get the idea. We could do the same for any type we like, whether we're using built in types or our own types (e.g. classes), as long as the type supports the < operator. All we have to do is write the appropriate function for the specific type and Bob's your uncle.

If you look at the above functions though, you'll see the similarity in the structure of the functions themselves. The algorithm, if you like, is the same regardless of the data type we're using. In each case we go through the same steps to find the maximum value, it's just the type that differs.

C++, well known as supporting Object Oriented programming, provides support for Generic programming. It does so through the use of templates. (Ada is another example of a language that supports generic programming).

Using templates we only have to write the function once. We simply code the steps that are needed - and remember, those steps are the same regardless of data type. At compile time our program will be compiled with a version of the function that is appropriate for the data type we use.

Here's an example of a program using a templaterized version of a function for finding the maximum of two values:

#include <iostream>
#include <string>

using namespace std;

template <typename T>
inline const T& myMax &#40;const T& first, const T& second&#41;
    return first < second ? second &#58; first;

int main&#40;&#41;
    int a=3, b=99;
    cout << "a=3, b=99, myMax&#40;3,99&#41;=" << myMax&#40;a,b&#41; << '\n' << endl;
    double m=99.9, n=3.3;
    cout << "m=99.9, n=3.3, myMax&#40;99.9,3.3&#41;=" << myMax&#40;m,n&#41; << '\n' << endl;
    string s1&#40;"hello"&#41;, s2&#40;"world"&#41;;
    cout << "s1=hello, s2=world, myMax&#40;hello,world&#41;=" << myMax&#40;s1,s2&#41; << '\n' << endl;

The type is represented by T in our function. We call this function for int, double and string values, and the compiler handles it for us. We only wrote a single function, but there will be 3 versions of this function in our compiled program, one for each data type used.

Some people claim that templates lead to code bloat but this doesn't make much sense when you consider that if you hand coded it you'd still end up with three (different) versions of the function.

Generic programming opens up a whole new world of possibilities and can be very useful. But I have to repeat, it's a complex area and there is much to consider. My example above really is very basic, but hopefully gives you some idea what using templates is about.

Was that any use to you?

YES! Thank you so much ;)
A big help

Do you know if this applies to straight C also?

What was confusing me, going back a few years ago, is that my data structures programming professor didn't even mention templates. Another professor, however, was making everyone use them right off the bat just for the simple HELLO WORLD application.

C doesn't have templates. The C Standard was revised in 1999, known generally as C99, but templates weren't introduced.

I can understand them not featuring greatly a few years ago on your course. Although they may have been known about at that time, compiler support wasn't terribly good until quite recently. Even now few compilers have completely implemented templates although support is generally good. In addition, courses often lag behind technology. In fact, even now many C++ students are taught to use pre-standard C++ even though the standard has been around for over 4 years.

It would be overkill, and extremely confusing, to introduce templates for a hello world program. The only exception might be if the course was for students already experienced in the core C++ language but aimed at introducing templates on top of that.

I'm glad you found the post helpful.

If you want more information about C++ Templates you might want to download and read volumes 1 & 2 of Bruce Eckel's free electronic book, Thinking In C++.

Volume 1 has an introduction to templates, volume 2 goes into more detail.

Actually, these books are a useful C++ resource generally so it could well be worth downloading them.

For a good book on C++ Templates I would suggest the book C++ Templates, The Complete Guide by Vandevoorde and Josuttis would be worth checking out. Josuttis also wrote one of the "must have" C++ books, The C++ Standard Library, A Tutorial And Reference.

Well it certainly helped me...7 years later :)

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.