3 ms·
When you find that C++ templates are taking unacceptably long compile times, you often benefit from declaring an `extern template`. Some times I have CPP files
by cjhanks 10y ago
When you find that C++ templates are taking unacceptably long compile times, you often benefit from declaring an `extern template`. Some times I have CPP files which do nothing but declare `extern template` instantiations for types I infrequently change.
This was added in C++11.
In the header you state something like:
template <typename _Tp>
class Example {
};
extern template class Example<int>;
In some CPP file you write:
template class Example<int>;
At that point any calls to Example<int> will not be resolved by callers. They will simply expect the template to be available at link-time. Theoretically this could be slightly slower (I think), since method calls will cross translation units rather than being inlined. But it does cut down on compile times.
- ndh2 10y agoExcept that extern template is unfortunately completely useless for any STL or Boost classes, which produce 99% of all the code bloat: https://msdn.microsoft.com/en-us/library/by56e477.aspx https://msdn.microsoft.com/en-us/library/by56e477.aspx > Note: The extern keyword in the specialization only applies to member functions defined outside of the body of the class. Functions defined inside the class declaration are considered inline functions and are always instantiated.