[LC++]template and gcc
Chris Vine
chris at cvine.freeserve.co.uk
Tue Mar 26 08:51:06 UTC 2002
On Monday 25 March 2002 11:04 am, Vincent Penquerc'h wrote:
> > class Dummy: public run<int> {} dummy;
>
> You can also simply instantiate your template in an object file,
> to tell the compiler that you do use it, so it can generate the
> right code:
>
> #include "run.h"
>
> template class Running<int>;
>
> This is as when you do:
>
> void foo(void);
>
> to tell the compiler there is something that's called foo,
> except that the compiler will instanciate the template (generate
> code for it templated for int).
I used to adopt the brute force method of instantiating a template
by deriving from it because it was the only way I could be sure of getting
gcc-2.7.2 to generate the required object code (the "template class
Running<int>;" approach wasn't reliable with that compiler). I also remember
when Borland C++/Turbo C++ used to do it by means of dummy typedefs (this was
the CFront 2.5 or 3.0 days).
I don't bother now to use a separate object file to instantiate required
templates (and I suspect it is very rare to instantiate templates this way in
code written nowadays) because it is a hassle to keep things in sync that
way, particularly with the adoption of the STL. Presumably linkers have got
a lot smarter in adopting techniques to avoid duplicate code.
Chris.
More information about the tuxCPProgramming
mailing list