This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: interface/implementation: export keyword?
- To: egcs at cygnus dot com
- Subject: Re: interface/implementation: export keyword?
- From: Nathan Myers <ncm at cygnus dot com>
- Date: Thu, 29 Oct 1998 16:21:34 -0800
- Newsgroups: cygnus.egcs
- Organization: http://www.cantrip.org/
- References: <71B30885B657D111809D080009EEBBF339EABC.cygnus.egcs@MAILSERV.molienergy.bc.ca>
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