Given a class template like:
template<typename T>
class a {
public:
void f();
};
template<typename T>
void a<T>::f() {
...
}
C++11 has explicit class template instantiation declarations:
extern template class a<int>;
To define that a translation unit encountering a<int> should not try to instantiate it, but instead look for an existing (explicit) instantiation to link to. In a shared library, this corresponding template instantiation definition would be put in a module of the library:
template class a<int>;
This is useful for example when the shared library is compiled with more compiler optimization, with OpenMP, ... than the program using it.
But currently, template class a<int>; does not instantiate the template class's member function definitions (only the declarations). So to also include an instantiation of a<int>::f this would be needed: In the header:
extern template class a<int>;
extern void a<int>::f();
and in the source
template class a<int>;
void a<int>::f();
It may be useful to be able to state in the template class definition that when the class template gets explicitly instantiated, some of its member functions, or other related classes/functions should get too. Something like
template<typename T>
class a {
public:
using helper = helper<T>;
void f();
template<typename U> void g();
extern helper;
extern f();
extern g<int>();
extern g<long>();
};