interface/implementation: export keyword?
Nathan Myers
ncm@cygnus.com
Fri Oct 30 03:25:00 GMT 1998
Jan Reimers wrote:
> > > > 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: ...
It's better to conditionally include the template definitions in the
headers directly, as:
// foo.h
#ifndef INCLUDED_FOO
# define INCLUDED_FOO
template <class T> void mangle(T*);
# ifdef EXPORT_NOT_IMPLEMENTED
# define export
# include "bar.h" /* needed for mangle() definition */
# include "foo.tcc"
# endif
#endif
// foo.tcc
export template <class T>
void mangle(T*) {}
This is the solution used in the libstdc++-v3 project.
When export is supported then you read the ".tcc" files
into your template repository. In the meantime compilation
is slower because each compilation unit sees the whole
definition.
> > Surely there must be an elegant way of doing this already, or does
> > nobody else use templates and require them to be space-efficient?
>
> I would guess that very few people don't use templates these days. The
> standard is full of them, even istream and ostream are implemented with
> templates.
It's not a space-efficiency issue; it's a matter of convenience,
size of intermediate files, and compilation time. The solution
to these is to implement export.
Nathan Myers
ncm@cantrip.org
More information about the Gcc
mailing list