Re: template code size optimization
The common optimization function inlining is a space/time trade-off
and breaks the binary interface, so cannot be done for exported
functions, but it is still done. Neil, yes, if you instantiate a
template only once, it does not produce more code than a non-template.
But templates are easily instantiated more than once, and the
programmer may not be aware of the associated code bloat.
But templates have a semantics side, static typing, and a code
generation side, and compilers could separate the two. For example,
some cases of CRTP (http://en.wikipedia.org/wiki/
Curiously_Recurring_Template_Pattern) can be replaced by virtual
functions:
template<class Derived>
struct Base {
// needs to be instantiated for each Derived::Func2 implementation
int Func() {
// ... lots of code here, so inlining not useful
return static_cast<Derived*>(this)->Func2();
}
};
can be replaced by
struct Base {
virtual int Func2()=0;
// code only necessary once:
int Func() {
return Func2();
}
};
The first produces more code, the second less code, at the expense of
a virtual function table with each instance and dynamic dispatch for
the call.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]