TITLE: Do templates cause code bloat? PROBLEM: Jan_Walter@mindlink.bc.ca (Jan Walter) [...] You're telling me that if under compiler X that I have a list template and do the following: List li; List lo; List lf; That 3x the amount of code is not going to be generated? Or that 3x the amount of code MAY be generated? I would think that this is highly dependent on the compiler, but have a feeling that, yes, 3x the amount of code is going to be generated regardless. RESPONSE: bs@alice.att.com (Bjarne Stroustrup), 21 Mar 94 AT&T Bell Laboratories That depends primarily of how List is defined. If List stores a COPY of its argument, code replication is the obvious and often the best implementation technique. There is no single best strategy for implementting such templates. What is ``best'' depends critically on the definition of the template, the use of the templates, the users needs in terms of time, space, re-compilation overheads, etc. There was a nice PhD thesis from CMU demonstrating that about ten years ago, but I don't recall the title. However, if - as is not uncommon - the list stores a pointer to the argument, no code replication is necessary. This point/problem was specifically considered in the design of templates. The basic technique is to use inheritance to eliminate the need for code replication. See sec13.8 [ Interface Classes ] of Bjarne Stroustrup: The C++ Programming Language (2nd edition) Addison-Wesley ISBN 0-201-53992-6. 1991. or sec15.5 of Bjarne Stroustrup: The Design and Evolution of C++ Addison Wesley, ISBN 0-201-54330-3. March 1994.