How to use explicit template instantiation to reduce compilation time?

Declare the instantiation in the header:

extern template class A<int>;

and define it in one source file:

template class A<int>;

Now it will only be instantiated once, not in every translation unit, which might speed things up.


If you know that your template will be used only for certain types, lets call them T1,T2, you can move implementation to source file, like normal classes.

//foo.hpp
template<typename T>
struct Foo {
    void f();
};

//foo.cpp
template<typename T>
void Foo<T>::f() {}

template class Foo<T1>;
template class Foo<T2>;