C++ Templates – Handling Long Compile Times

ctddtemplates

I use Visual Studio 2012 and he have cases where we added templates parameters to a class "just" in order to introduce a "seam point" so that in unit-test we can replace those parts with mock objects.

How do you usually introduce seam points in C++: using interfaces and/or mixing based on some criteria with implicit interfaces by using templates parameters also ? One reason to ask this is also because when compiling sometimes a single C++ file (that includes templates files, that could also include other templates) results in an object file being generated that takes in the order of around 5-10 seconds on a developer machine.

VS compiler is also not particularly fast on compiling templates as far as I understand, and because of the templates inclusion model (you practically include the definition of the template in every file that uses it indirectly and possibly re-instantiate that template every time you modify something that has nothing to do with that template) you could have problems with compile times (when doing incremental compiling).

What are your ways of handling incremental(and not only) compile time when working with templates (besides a better/faster compiler :-)).

Best Answer

If your templates parameters can only assume a finite (and small) set of values, you could move their definition in a source file, and use explicit instantiation.

For instance, in aaa.h you only declare the template functions f and g:

template <int n>
int f();

template <class T>
void g(int a);

Assume n template parameter can only be 1, 3, 6, and T template parameter can only be int, long and void *.

Then you define them in aaa.cpp like this:

template <int n>
int f()
{
    ...
}

template <class T>
void g(int a)
{
    ...
}

template int f<1>();
template int f<3>();
template int f<6>();

template void g<int>(int a);
template void g<long>(int a);
template void g<void *>(int a);

In this way the compiler instantiates the template for the given parameters when compiling aaa.cpp. When compiling client code, it assumes that the definitions exist somewhere, and the linker will take care of that.

#include "aaa.h"

int main()
{
    f<1>();
    f<3>();
    f<6>();

    g<int>(5);
    g<long>(5);
    g<void *>(5);
}

You can explicitly instantiate template classes too. The drawback is that you can not use f or g with other template parameters.

#include "aaa.h"

int main()
{
    f<5>();
}

results in

undefined reference to `int f<5>()'

I used this technique in a project where few complex classes depended on a small (< 10) set of integer template parameters, and it significantly reduced compilation time (since the compiler did not have to parse the complex template definitions when compiling the client code). Of course you may get lesser improvements, depending on the actual code.

Related Topic