Project structure: explicit template instantiation

  • Thread starter Patrick Kowalzick
  • Start date
P

Patrick Kowalzick

Hello all,

I have a templated class. This class is a parent class of quite some classes
spread over a projects. As changes inside member-function definitions force
a recompile of many files, I search a way around the lack of the
export-functionality.

I started to seperate the declaration and definition:

*** template_class_declaration.h ***

template <class T>
class A
{
public:
void foo();
};

*** file end ***
*** template_class.h ***

#include "template_class_declaration.h"

template <class T>
void A<T>::foo()
{
doSomething();
}

*** file end ***


Like this I can still use
#include "template_class.h"
like before.

For somehow "known-types" (by the implementor) I added another file for
explicit instantiation:


*** template_instantiation.cpp ***

#include "template_class.h"
#include "all_neccessary_includes.h"

template class A<SomeOtherClass>;

*** file end ***


For files using these explicit instanciated classes, I can change the
include directive from
#include "template_class.h"

to
#include "template_class_declaration.h"


Like this I do not need complete recompilation for changes in the
definition. For my first small tests this works fine.

What do you think about this structure? Did I overlook something? What do
you do in these cases?

Kind regards,
Patrick
 
C

Chris Theis

Patrick Kowalzick said:
Hello all,

I have a templated class. This class is a parent class of quite some
classes spread over a projects. As changes inside member-function
definitions force a recompile of many files, I search a way around the
lack of the export-functionality.

I started to seperate the declaration and definition:

*** template_class_declaration.h ***

template <class T>
class A
{
public:
void foo();
};

*** file end ***
*** template_class.h ***

#include "template_class_declaration.h"

template <class T>
void A<T>::foo()
{
doSomething();
}

*** file end ***


Like this I can still use
#include "template_class.h"
like before.

For somehow "known-types" (by the implementor) I added another file for
explicit instantiation:


*** template_instantiation.cpp ***

#include "template_class.h"
#include "all_neccessary_includes.h"

template class A<SomeOtherClass>;

*** file end ***


For files using these explicit instanciated classes, I can change the
include directive from
#include "template_class.h"

to
#include "template_class_declaration.h"


Like this I do not need complete recompilation for changes in the
definition. For my first small tests this works fine.

What do you think about this structure? Did I overlook something? What do
you do in these cases?

Kind regards,
Patrick


Hi Patrick,

well if "excessive" recompilation is really a problem then explicit
instantiation is the way to go. So, you're doing fine there.

Cheers
Chris
 
P

Patrick Kowalzick

I have a templated class. This class is a parent class of quite some
classes spread over a projects. As changes inside member-function
definitions force a recompile of many files, I search a way around the
lack of the export-functionality.

I started to seperate the declaration and definition:

This topic is as well discussed in "C++ Templates" from
Josuttis/Vandevoorde.

They just propose a different naming scheme. Instead of
"template_class_declaration.h" & "template_class.h"

they use
"template_class.h" & "template_classdef.h" (i prefer
"template_class_definition.h").

I will stick to the solution they proposed.

Regards,
Patrick
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top