问题
Is it possible to forbid implicit instantiation, like -fno-implicit-templates
does, but only for one template?
I have a problem with implicit instantiation of incomplete template, which causes compilation failure (part of implementation is hidden in source file, and I don't want to have it in other TUs). -fno-implicit-templates
solves the problem, but at cost of problems with using STL and other templates.
回答1:
You can try to use explicit template instantiation. Put explicit template instantiation declaration extern template class TemplateClass<ArgumentsSet>;
(where ArgumentsSet
is a TemplateClass
arguments set for which you want to avoid implicit instantiation in your code) in your header file (you can put such directive for several arguments sets if you want). Also put explicit template instantiation definition template class TemplateClass<ArgumentsSet>;
in your source file to explicitly instantiate TemplateClass
for ArgumentsSet
in this translation unit.
来源:https://stackoverflow.com/questions/22768865/can-i-use-fno-implicit-templates-feature-only-for-one-template