Templates: separating interface from implemenation

B

barcaroller

I have a template class that has become overly complex and unwieldy. I
would like to break it up into an interface (*.h) and one or more
implementation files (*.cpp). The compiler will not let me do that. Is
there a recommended work-around to break up a large template class into
multiple files?
 
J

Jerry Coffin

I have a template class that has become overly complex and unwieldy. I
would like to break it up into an interface (*.h) and one or more
implementation files (*.cpp). The compiler will not let me do that. Is
there a recommended work-around to break up a large template class into
multiple files?

The theoretically correct idea is to use the 'export' keyword -- but
your compiler probably doesn't support it (only one, Comeau C++,
does).

Lacking that, you have a few choices. If you _just_ want separate
files, you can move pieces of code into separate files, and have a
header that contains the template definition, and includes the
definitions of individual functions as needed. This still works
pretty much like one huge header though -- in particular, your source
files still depend on all the pseudo-headers, and any change in any
of them forces recompilation of the source files.

You can get away from that under a _few_ circumstances. For example,
if your templates are primarily collections of T, you can (at least
sometimes) get away with writing some base code that works with
pointers to void, and then write a small template that takes care of
casting from T * to void * and back as needed. At one time this was
quite common, but now that most people primarily just use the
containers in the standard library, it's not useful very often.
 
K

KjellKod

You can use the following for taking care of it whether or not
'export' keyword is used.
Example below simplified:

In somefile.h
=====================================
template<class Parameter>
class SomeClass
{
public:
....
void aFunc(Parameter param);
...
};

#ifndef USE_EXPORT_KEYWORD // Fix to ensure that template .h & .cpp
files are
#include "somefile.cpp" // the same compilation unit. If the
'export' keyword
#endif // is not supported by the compiler
the .cpp file is
// simply included.



In somefile.cpp
===============================

#ifndef USE_EXPORT_KEYWORD
#define export /* nothing */
#endif

export template<class Parameter>
void SomeClass<Parameter>::aFunc(Parameter param)
{
...
}
 
R

red floyd

In somefile.cpp
===============================

#ifndef USE_EXPORT_KEYWORD
#define export /* nothing */
#endif

   export template<class Parameter>
   void SomeClass<Parameter>::aFunc(Parameter param)
   {
     ...
   }

Redefining keywords is generally not a good thing. I'd rewrite it as:

#ifndef USE_EXPORT_KEYWORD
#define EXPORT
#else
#define EXPORT export
#endif

EXPORT template<class Parameter>
void SomeClass<Parameter>::aFunc(Parameter param)
{
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top