>>>>> "Jay" == Jay Pipes <Jay.Pipes@stripped> writes:
Jay> Michael Widenius wrote:
Roy> Templates are already introduced - they need guidelines for when and why.
>>>> Templates are fine to use as long as they are used only to remove
>>>> casts in the code, not to generate code.
Jay> Templates remove casts by generating code.
>> One line for one line is fine (the same way as a define).
Jay> Actually, templates generate *less* code than a similar define, because
Jay> defines are *always* expanded, and templates (depending on the
Jay> compiler's implementation) usually only generate code when the linker
Jay> actually sees the template used for a specific type.
A define only generates code when it's used. Same goes with a
template. With most compilers there is no difference in amount of
code generated for identically used templates or defines.
To be exact, the compiler generates either generate code for all
template usage and all combination or uses a database to store all
seen combinations. The linker then includes once copy of those parts
that are used.
>> And just enforcing types doesn't actually generate any assembler code
>> when comparing with the original code; It just removes compiler checks.
Jay> And thus makes the code less safe and more error-prone.
Actually it makes the code safer and less error prone as without the
usage of templates you would instead have to use explicite casts that
are unsafer than the casts that you do inside the template in c++
friendly way. Just look at the list code for an example of this.
>> As long as this is the case, tools like gcov and gdb etc works fine.
>> Things starts to fall appart when you have templates that generates
>> many lines of code as then you can't be sure what code are used and
>> debugging also gets to be more difficult.
Jay> I agree that debugging gets more, say, verbose. :) But I'm always able
Jay> to see in GDB what code is being executed...
GDB doesn't work good for large templates and especially not for
templates that includes templates. For an example of this, try to
debug the old MySQL c++ client library. It's almost impossible with
>> Not forgetting about code
Jay> An example would work wonders here. Otherwise, I will start to think
Jay> you're still thinking about compilers from >5 years ago :)
Please read how templates are expanded. Some open source C++ have
been abounded just because they used templates wrongly and got so much
generated code that the end result was too slow to be usable.
(Talk with the gnome people about this)
Every different type combination you use for a template is expanded to
it's full copy of the code. A common example is using templates to
implement optimal qsort for different types. In this case if you have
7 different types (long, short, string, double etc..) for the
arguments to qsort, you get 7 different qsort implementations.