MikeP said:
I can only lead you to the water. I can't make you drink it. Suit
yourself.
I still don't understand what this "template code bloat" is.
Does templated code require more code than non-templated code?
Well, if we compare it to *one* non-templated function or class, there
indeed is at least one additional line of code (the "template" part).
Also, every member function of a template class, if not implemented in
the class declaration, will require the additional "template" line as
well. Also, there will be additional "<type>" specifications. This indeed
increases the amount of code somewhat, but not very radically.
However, if we compare the *generated* code with the equivalent
non-templated versions, the amount of code is immediately smaller in
the templated version, rather obviously, because there will be no
code repetition. A non-templated function or class will have to be
duplicated for each type. This would indeed be code bloat. In fact,
templates are helping *reduce* code bloat, not increase it. So I don't
really understand your point.
Or are you talking about the executable binary? Do you mean that the
end result ends up significantly larger than when not using templates?
My question would be: Compared to what?
If you use a templated function or class with one type, the generated
code will be pretty much the same as if a non-templated version of that
function or class had been used with that specific type. I don't see how
the executable binary would get any larger. (If the binary includes
debugging information then it may be larger because mangled template
names are usually larger than non-templated ones. However, that would be
a pretty silly argument because the debug information is there for a
debug build of the project, but not intended for the final distribution
build.)
If you use the same templated function or class with two types, the
code will be duplicated (although optimized for each type individually,
possibly resulting in a smaller result for one type than the other).
What would be a non-templated alternative? Most obviously, explicitly
duplicating the source code for each of the two types. The resulting
binary would still be pretty much the same as with the templated version,
so exactly what would be the difference?
In some situations you could try to make a (somewhat) generic but
non-templated implementation, as is typical in C. Almost invariably
this will introduce inefficiency and lack of type-safety compared to
the templated version (see qsort() for a practical example). Rather
ironically, in some situations it might even produce a *larger* binary
than the templated version (because the templated version can usually
be inlined by the compiler, possibly optimizing entire function calls
away and tightly optimizing and packing their code at the calling site,
embedded and merged with the surrounding code).
Nevertheless, even in situations where heavily templated code truly
generates a larger executable binary by a significant margin compared to
some non-templated alternative, is it really that catastrophic? Maybe in
some embedded systems with very little RAM it could be an issue, but the
vast majority of C++ code out there is not made for such systems. In a
typical desktop machine it makes little difference whether the executable
binary is 1 MB or 1.5 MB in size (assuming the templated code makes that
much of a difference).
It is still unclear to me what exactly is it that you mean by "template
code bloat".