943,503 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6557
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 8th, 2002
0

C++ Templates

Expand Post »
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.
Similar Threads
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Dec 11th, 2002
0
Re: C++ Templates
Ahh. Too much to write. It's better if I make a tutorial out of this.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Dec 12th, 2002
0
Re: C++ Templates
So are you gonna? I need to put the C++ tutorials I wrote on this site.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Dec 12th, 2002
0
Re: C++ Templates
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.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Dec 21st, 2002
0
Re: C++ Templates
Any luck finding them?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Dec 21st, 2002
0
Re: C++ Templates
I never looked. I think it's in an old box in the garage. I still have to look. 8)
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Feb 1st, 2003
0

Re: C++ Templates

Quote originally posted by cscgal ...
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?
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Feb 1st, 2003
0
Re: C++ Templates
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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Feb 1st, 2003
0
Re: C++ Templates
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:

C++ Syntax (Toggle Plain Text)
  1. int maxInt(int x, int y)
  2. {
  3. return x < y ? y : x;
  4. }

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:

C++ Syntax (Toggle Plain Text)
  1. double maxDouble(double a, double b)
  2. {
  3. return a < b ? b : a;
  4. }

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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>
  7. inline const T& myMax (const T& first, const T& second)
  8. {
  9. return first < second ? second : first;
  10. }
  11.  
  12. int main()
  13. {
  14. int a=3, b=99;
  15. cout << "a=3, b=99, myMax(3,99)=" << myMax(a,b) << '\n' << endl;
  16.  
  17. double m=99.9, n=3.3;
  18. cout << "m=99.9, n=3.3, myMax(99.9,3.3)=" << myMax(m,n) << '\n' << endl;
  19.  
  20. string s1("hello"), s2("world");
  21. cout << "s1=hello, s2=world, myMax(hello,world)=" << myMax(s1,s2) << '\n' << endl;
  22. }

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.
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Feb 4th, 2003
0
Re: C++ Templates
Was that any use to you?
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Opening extremely large text files
Next Thread in C++ Forum Timeline: [C++] INeed help with guessing game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC