This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: interface/implementation: export keyword?



> > I'm trying to sensibly separate template definitions/declarations for
> > a project I'm writing in C++, as I believe (?) it's good practice to
> > separate the interface from the implementation.
> 
> The short answer is to put your template declarations in an .h file,
> and your template definitions in a .cxx (or .cc) file, and until
> egcs supports export, #include the .cxx file as well as the .h file
> when you use the templates.

Thanks for the advice, but there's two reasons I didn't do this in the
first place:

The first reason is that I couldn't get make to do proper dependency
checking without rewriting all the rules :-(

Also, I have a template Matrix class (say) which is used by two other
classes: they both use a Matrix<double>.  If I put the classes in
separate .cc files I get a bigger executable than if I put them in one
big .cc file, which (I imagine) means that my final executable has two
copies of the Matrix<double> code instead of one -- I'm not familiar
with the structure of the object code so I can't be sure.  Is there
any way of getting egcs (or the linker) to eliminate redundant
instantiations? 

I believe that this can be solved by explicitly instantiating the
needed classes in the template .cc file and linking against the
resulting .o file, but this requires some clever dependency checking
and preprocessor magic beyond my current skills.

How does the STL do it?  Does the compiler have to emit code for all
the template instantiations itself and put them in executables, or is
there some magic way libraries use to handle arbitrary types for
template instantiations?

Surely there must be an elegant way of doing this already, or does
nobody else use templates and require them to be space-efficient?

Thanks,

TJW

--------------------------------------------------------------------
Terence J. Wells                E-mail  : tjw30@cam.ac.uk
St John's College               Phone   : +44.468.916617 (mobile)
Cambridge CB2 1TP               Room    : A3 New Court, SJC
United Kingdom                  Talk    : tjw30@tjw30.joh.cam.ac.uk



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]