[Previous]  [Up]  [Next]

CFront problems

Template instantiation



Unfortunately CFront does not automatically instantiate templates, so the task falls to the user. CathLibCPP provides several aids to help with instantiating both class and function templates.

General templates

A suite of macros is provided in 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.

Instantiation of container templates

As mentioned earlier in the section on template code bloat, the instantiation of container templates, needs more detailed explanation. The problem here is that it is not merely the container templates that need to be instantiated, but also the templated helper classes that allow the hoisting mechanism to work.

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.

Containers of standard types

To make use of the library a little more straightforward, CathLibCPP provides pre-instantiations of the 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.

 [Previous]  [Up]  [Next]