【发布时间】:2011-09-12 23:15:21
【问题描述】:
给定一个类似的模板
template<int dim> class Point { ... };
这个模板可以像这样显式地实例化
template class Point<0>;
template class Point<1>;
template class Point<2>;
template class Point<3>;
我不想像上面那样单独实例化每个模板,我想用一个调用递归地实例化它们
template class RecursiveInstantiate<Point, 3>;
RecursiveInstantiate<T, i> 将实例化 T<i>、T<i-1>、...、T<0>。是否有可能创建这样一个类RecursiveInstantiate?如果不可能,您知道使用预处理器的方法吗?
事实上,对于具有多个模板参数的类(例如Node<int i1,int i2,int i3>),对于 {0,1,2,3} 中的 i1,i2,i3 的所有组合,我很感兴趣。但我希望能够自己完成第二部分。
感谢任何建议,以及解释为什么我想要实现的目标是不可能的。
更新:到目前为止感谢您的 cmets。我现在更清楚地看到了问题的真正所在。线
template class Point<3>;
实例化模板并将其符号导出到目标文件。表单的实例化
template class RecursiveInstantiate<Point, 3>;
可以实例化类class Point<3>、class Point<2>、...。但显然这只发生在本地。模板不会导出到目标文件。也许我将不得不寻找使用预处理器的解决方案。
正如我现在看到的那样,我一开始没有足够准确地提出我的问题,我感谢您的回答和选择的正确答案。
注意:我正在使用 g++/clang 作为编译器的 linux 上尝试这个。
【问题讨论】: