C++ Templates

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2002
Posts: 12,038
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

C++ Templates

 
0
  #1
Dec 8th, 2002
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.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 6
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor
 
0
  #2
Dec 11th, 2002
Ahh. Too much to write. It's better if I make a tutorial out of this.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,038
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #3
Dec 12th, 2002
So are you gonna? I need to put the C++ tutorials I wrote on this site.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 6
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor
 
0
  #4
Dec 12th, 2002
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.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,038
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #5
Dec 21st, 2002
Any luck finding them?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 6
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor
 
0
  #6
Dec 21st, 2002
I never looked. I think it's in an old box in the garage. I still have to look. 8)
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: C++ Templates

 
0
  #7
Feb 1st, 2003
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,038
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #8
Feb 1st, 2003
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.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member
 
0
  #9
Feb 1st, 2003
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:

  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:

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member
 
0
  #10
Feb 4th, 2003
Was that any use to you?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC