Callbacks in C++


factor templates

problem:

Each time a template is used with different types, the compiler makes a copy of the template code.

context:

Creating a template class in C++.

forces:

solution:

Factor type independent implementation details into a non-templated base class or implementation class.

forces resolved:

The benefits of templates are kept while minimizing the cost by reducing the amount of code the compiler must duplicate.

design rationale:

Usually a lot of the code in a templated class is independent of the type the template is instantiated with. This code can be implemented in a separate class that is not a template. Even more code can be moved to a base class by giving the base class pure virtual methods that can be implemented by the template when types are known. If an implementation class is used for factoring instead of a base class, often data can be cast to generic types so it can be stored and operated on by the implementation class.

If less code is in templated classes, then less code will be generated when templates are instantiated.

Note that more aggressive template factoring can be performed by applying the pattern Type Destroyer is Restorer [appendix A].

[View/Add Comments]


Callbacks in C++
Copyright © 1996-2000 Paul Jakubik
Created: 26 July 1996. Last update: 26 November 2000.
pauljakubik@yahoo.com