[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: using templates and separating the files in .h and .cpp
From: |
aaragon |
Subject: |
Re: using templates and separating the files in .h and .cpp |
Date: |
14 Nov 2006 19:49:18 -0800 |
User-agent: |
G2/1.0 |
Paul Pluzhnikov wrote:
> "aaragon" <alejandro.aragon@gmail.com> writes:
>
> > I'm using the g++ compiler, and I run into a problem when I try to
> > separate the declaration in the .h file from the implementation in the
> > .cpp file. It seems that when I do this, I have linking problems (only
> > when I use templates in the declarations). Does anyone know why is
> > this?
>
> Because gcc doesn't support external templates (few (if any) other
> compilers do).
>
> Some compilers play tricks: when you '#include "foo.h"' which
> declares templates, they "automagically" look for foo.cpp, and
> include it in the current compilation unit, saving resulting
> instantiated methods in a template "repository".
>
> All such schemes (in my experience) are buggy and eventually cause
> extreme pain, when partial rebuilds fail to link, because repository
> is not updated correctly under some conditions.
>
> On ELF platforms none of this trickery is necessary anyway, because
> template bodies are put into linkonce sections, so final executable
> contains only one copy of the instantiated method (i.e. you don't
> get code bloat).
>
> > I ended up compiling only the .cpp files (which have an #include
> > "respective_file.h" only) and putting all the implementation details in
> > the .h files.
>
> That's the most portable approach.
>
> Another alternative is to manually manage instantiation via explicit
> instantiation requests.
>
What do you mean by "explicit instantiation requests"?
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.