问题描述
是否可以禁止隐式实例化,例如 -fno-implicit-templates
,但只限于一个模板?
Is it possible to forbid implicit instantiation, like -fno-implicit-templates
does, but only for one template?
我有一个隐式实例化不完整模板的问题,这导致编译失败(实现的一部分隐藏在源文件中,我不想在其他TU中有它)。 -fno-implicit-templates
解决了这个问题,但是使用STL和其他模板时出现问题。
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.
推荐答案
您可以尝试使用显式模板实例化。放置显式模板实例化声明 extern template class TemplateClass< ArgumentsSet> ;;
(其中 ArgumentsSet
TemplateClass 参数集,你想避免在代码中隐式实例化)在你的头文件(你可以把这样的指令为几个参数集,如果你想)。还在源文件中放置显式模板实例化定义模板类TemplateClass< ArgumentsSet> ;;
,以明确实例化 TemplateClass
c $ c> ArgumentsSet 在此翻译单元中。
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.
这篇关于我可以对一个模板使用`-fno-implicit-templates`功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!