help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: using templates and separating the files in .h and .cpp


From: Paul Pluzhnikov
Subject: Re: using templates and separating the files in .h and .cpp
Date: Tue, 14 Nov 2006 19:29:36 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

"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.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]