| | |
C++ Templates
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
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.
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.
•
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
•
•
•
•
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.
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.
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. •
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
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:
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:
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:
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.
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)
int maxInt(int x, int y) { return x < y ? y : 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:
C++ Syntax (Toggle Plain Text)
double maxDouble(double a, double b) { return a < b ? b : 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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; template <typename T> inline const T& myMax (const T& first, const T& second) { return first < second ? second : first; } int main() { int a=3, b=99; cout << "a=3, b=99, myMax(3,99)=" << myMax(a,b) << '\n' << endl; double m=99.9, n=3.3; cout << "m=99.9, n=3.3, myMax(99.9,3.3)=" << myMax(m,n) << '\n' << endl; string s1("hello"), s2("world"); cout << "s1=hello, s2=world, myMax(hello,world)=" << myMax(s1,s2) << '\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.
![]() |
Similar Threads
- Free High Quality Website & Logo Templates (Post your Resume)
- Great Place for free templates and other resources. (Site Layout and Usability)
- More psd templates (Website Reviews)
- RTC Templates - Talented designers required (Software Development Job Offers)
- Templates (Websites for Sale)
Other Threads in the C++ Forum
- Previous Thread: Tutorials
- Next Thread: Operating Systems assignment
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







