On Mon, Feb 02, 2004 at 02:21:39PM +0100, Laurens wrote: > Do templates have any significant impact on heap memory usage for instance? > Code size? Any caveats I should be aware of?
A C++ template is just an automatic way of generating code that is mostly repetitive, but just with different parameters. Think of it as a really powerful macro utility. When you declare a vector<int>, behind the scenes, C++ is basically creating a new class named "vector_of_int", generating code for that class based on the template declaration, and compiling it and adding it to the object file. You could do the same thing using some kind of preprocessor for a language that doesn't support templates. Once you understand that, it should be plain that the biggest implication is code bloat. If you use a template with a three different classes in your program (e.g., vector<char>, vector<int>, vector<FormType*>, etc.), you are essentially creating 3 new classes, and all of the code for each of these "classes" is added to your application. This will obviously lead to code bloat compared to the alternate solution where you use a non-template generic vector class whose members are all void* pointers. The problem with the latter approach is type safety. A very common approach, especially for containers, is to write a generic class that uses void pointers. The bulk of class logic goes here, so it'll only end up in your executable one time. Then, you write a templated helper class that acts as the intermediary for the generic cast and handles all the type casting duties. -- Dave Carrigan Seattle, WA, USA [EMAIL PROTECTED] | http://www.rudedog.org/ | ICQ:161669680 UNIX-Apache-Perl-Linux-Firewalls-LDAP-C-C++-DNS-PalmOS-PostgreSQL-MySQL -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/