|
|
CFront problemsTemplate instantiation |
|
tpltutil.h to assist with manual instantiation of non-inline member and non-member template functions.
To instantiate a template class some_class<T1, ... Tn> which has non-inline member functions create a seperate .c++ file of the form:
#include "tpltutil.h"
// #includes/forward declarations of parameter types
#include "someclass.c++" // out of line definitions
INSTANTIATE_CLASS_N(some_class, T1, ... Tn)
For example:
#include "tpltutil.h"
class Foo;
#include "vector.c++" // out of line definitions
INSTANTIATE_CLASS_1(vector, Foo*) // vector of Foo*'s
To instantiate a template non-member function some_fn(T1 ... Tn) create a seperate .c++ file of the form:
#include "tpltutil.h"
// #includes/forward declaration of parameter types
#include "somefn.c++" // out of line definition
INSTANTIATE_FN_N(some_fn, T1, ... Tn)
For example:
#include "tpltutil.h"
class Foo;
#include "algorithm.c++"
INSTANTIATE_FN_3(for_each, Foo**, Foo**, Foo*)
Examples of the template instantiation mechanism can be found in testsuite.c++.inst00 ... inst02. This is managable, but not particularly convenient.
For single translation unit programs, it is usually sufficient to just #include "hall.c++". However, for larger programs care needs to be taken to only instantiate each template once.
For each container class T there is a file hT.c++ that defines two macros INSTANTIATE_T(...) and INSTANTIATE_DESTROY_T(...) for instantiating the template; the latter also defines destroy() as described in the section on template semantics.
For example:
#include "hvector.c++"
class Foo;
INSTANTIATE_DESTROY_VECTOR(Foo);
instantiates a vector of Foos and defines a destroy(Foo *p) function.
deque, list and vector template classes for all of the builtin types and the string class, and for pointers to each of those. These pre-instantiations accounts for much of the size of the library.