Вопрос

In my previous question I asked is recursive explicit template instantiation possible. I saw that it is indeed possible; however, this instantiation turns out to be effective locally only, the symbols of the recursively instantiated template are not exported to the object file and thus do not appear in the (shared) library. So I ask question here more precisely as in my previous post:

Given a template like

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>;

which exports the symbols of Point<0>, ..., Point<3> into the object file of the current translation unit. Instead of instantiating every template individually like above, I would like to instantiate them recursively with just one call.

Any solutions that achieves this is fine, be it in the style of template meta-programming, via a helper class like

template class RecursiveInstantiate<Point, 3>;

or via the preprocessor. Here I looked into the boost preprocessor library, which seems to have some loop constructs. However, I never used the boost preprocessor library (any advice is appreciated) but on a first glance I am skeptical if the loops can be used together with an explicit template instantiation.

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


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. As usual I want to use the explicit instantiations to speed up the compilation times by only defining the templates in one translation unit, thus I need the methods to template to be exported in the object file.

I hope for a compiler independent solution, but if that is not possible I need it for Linux with g++/clang.


See below for a survey of the solutions that I got and the final solutions that I made out of this.

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top