Templates
I have been programming with C++ for quite a while now but I have one question. What exactly is a template and what are the advantages/disadvantages of them. I''ve just come across them lately and to me they have been more of a pain in the butt than anything else. Thanks.
Mat
MatDoodah2001@hotmail.comLife is only as fun as you make it!!!
lets say you are writing a program that uses lots of linked lists, but with different data types. Rather than writing and debugging hundreds of different linked list implementations, you write one and make it a template. You can then instantiate a linked list that will store any data types you have or can think of in the future. Or you could save yourself the bother of writing anything at all and use STL.....
My definition would be parameterized data type (or function). Templates specify how a class (or function) can be generated by the compiler given a suitable set of template arguments. The main advantage is that it allows you to write a general implementation and make the compiler generate code for specific cases. You write a smaller amount of more reusable source code, which compiles to object code that is just as efficient.
Templates are cool and extremely powerful, but there are, of course, disadvantages. The mechanisms can be difficult to master, the syntax can get a little bit hairy, and improper use often lead to bloated code. Portability is a concern too; support for templates are still spotty (especially true for Visual C++).
Templates are cool and extremely powerful, but there are, of course, disadvantages. The mechanisms can be difficult to master, the syntax can get a little bit hairy, and improper use often lead to bloated code. Portability is a concern too; support for templates are still spotty (especially true for Visual C++).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement