解决方法
如果显式实例化类模板,则将实例化所有非模板成员,包括静态数据成员,只要它们也有定义即可.例如:
template <typename T> struct foo { static int static_data; void non_template_member() {} template <typename S> void template_member(S) {} }; template <typename T> int foo<T>::static_data = 0; template struct foo<int>; template struct foo<double>;
底部的显式实例化为类型int和double创建static_data和non_template_member()的定义. template_member(S)没有定义,因为它仍然是一个开放集.
如果没有为static_data提供[模板化]定义,则不会实例化相应的定义.
该标准的相关部分是14.7.2 [temp.explicit]第8段:
An explicit instantiation that names a class template specialization is also an explicit instantiation of the same kind (declaration or definition) of each of its members (not including members inherited from base classes and members that are templates) that has not been prevIoUsly explicitly specialized in the translation unit containing the explicit instantiation,except as described below.
如果没有成员定义,则仅声明静态成员,并且显式实例化仅仅会看到实例化的声明.通过定义,显式实例化成为定义.