From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nathan Myers To: egcs@cygnus.com Subject: Re: interface/implementation: export keyword? Date: Fri, 30 Oct 1998 03:25:00 -0000 Message-id: <3639068E.87C69293@cygnus.com> References: <71B30885B657D111809D080009EEBBF339EABC.cygnus.egcs@MAILSERV.molienergy.bc.ca> X-SW-Source: 1998-10/msg01249.html 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 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 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