Recently when I was trying to optimize my include hierarchy I stumbled upon the file a.hpp
:
template<class T>
class A
{
using t = typename T::a_t;
};
class B;
extern template class A<B>;
which seems to be ill-formed. In fact it seems as if the extern template statement at the end causes an instantiation of A<B>
which causes the compiler to complain about an incomplete type.
My goal would have been to define A<B>
in a.cpp
:
#include <b.hpp>
template class A<B>;
This way I avoid having to include b.hpp
from a.hpp
which seems like a good idea to reduce compile time. However it does not work (a.hpp
on itself doesn't compile!) Is there a better way of doing this?
Note: Of course I could just not use explicit template instantiation but this is not what I want! I would like to "precompile" A<B>
to save compilation time if it were used, but if A<B>
is not used I don't want to include b.hpp
in every file that uses a.hpp
!