递归显式模板实例化可能吗? [英] is recursive explicit template instantiation possible?

查看:26
本文介绍了递归显式模板实例化可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个模板

template<int dim> class Point { ... };

这个模板可以像这样显式实例化

this template can be instantiated explicitly like

template class Point<0>;
template class Point<1>;
template class Point<2>;
template class Point<3>;

不是像上面那样单独实例化每个模板,我想通过一个调用递归地实例化它们

instead of instantiating every template individually like above, I would like to instantiate them recursively with one call like

template class RecursiveInstantiate<Point, 3>;

其中 RecursiveInstantiate 将实例化 T, T, ..., <代码>T<0>.是否有可能创建这样一个 RecursiveInstantiate 类?如果不可能,你知道用预处理器做的方法吗?

where RecursiveInstantiate<T, i> would instantiate T<i>, T<i-1>, ..., T<0>. Is it somehow possible to create such a class RecursiveInstantiate? If it is not possible, do you know a way to do it with the preprocessor?

事实上,我有兴趣将其概括为具有多个模板参数的类,例如 Node 用于 {0,1 中 i1,i2,i3 的所有组合,2,3}.但我希望能够自己完成第二部分.

In fact I am interested in generalizing this for classes with multiple template parameters likeNode<int i1,int i2,int i3> for all combination of i1,i2,i3 in {0,1,2,3}. But I hope to be able to work out this second part by myself.

感谢任何建议,以及解释为什么我想要实现的目标是不可能的.

Any advice, also an explanation why it is impossible what I want to achieve is appreciated.

更新:感谢您到目前为止的评论.我现在更清楚地看到问题的真正所在.线

Update: thank you for your comments so far. I see now more clearly where the problem really is. The line

template class Point<3>;

实例化模板并将其符号导出到目标文件.表单的实例化

instantiates the template and exports its symbols to the object file. An instantiation of the form

template class RecursiveInstantiate<Point, 3>;

可以实例化类 class Point<3>, class Point<2>, .... 显然这只会在本地发生.模板不会导出到目标文件.也许我将不得不寻找使用预处理器的解决方案.

may instantiate the classes class Point<3>, class Point<2>, .... Apparently this only happens locally though. The templates are not exported to the object file. Maybe I will have to look for a solution using the preprocessor.

正如我现在看到的,我一开始问的问​​题不够准确,我感谢您的回答和选择的正确答案.

As I see now that I did not ask my question precisely enough in the beginning, I appreciate your answers and selected ones as correct.

注意:我正在用 g++/clang 作为编译器的 linux 上尝试这个.

Note: I am trying this on linux with g++/clang as compilers.

推荐答案

你可以创建一个小的 Instantiator 类:

You could make a little Instantiator class:

template <unsigned int N> struct Instantiator
{
  Point<N> p;
  Instantiator<N-1> i;
};

template <> struct Instantiator<0>
{
  Point<0> p;
};

然后简单地添加一个显式实例化:template struct Instantiator<81>;

Then simply add one explicit instantiation: template struct Instantiator<81>;

您可以按字典顺序将这个想法扩展到任意数量的整数参数.

You can extend this idea lexicographically to any number of integral parameters.

正如@Georg 所说,让我们让它变得通用:

As @Georg says, let's make it generic:

template <template <unsigned int> class T, unsigned int N> struct Instantiator
{
  T<N> t;
  Instantiator<T, N-1> i;
};

template <template <unsigned int> class T> struct Instantiator<T, 0>
{
  T<0> t;
};

template struct Instantiator<Point, 82>;

这篇关于递归显式模板实例化可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆