If I have a class called Tuple<T, SIZE> and I want, let’s say, 20 different float templates instantiated when compiling the library (so that the user does not encounter linking errors):
template Tuple<float, 1>;
template Tuple<float, 2>;
...
template Tuple<float, 20>;
Is there any way to recursively perform the above? And maybe allow it to be flexible so that I can put it any number I would like and it instantiates the classes for me?
EDIT: What I have tried so far (does not seem to work):
template <typename T, unsigned int MAX_RANGE>
class AllTuples
{
Tuple<T, MAX_RANGE> y;
AllTuples<T, MAX_RANGE - 1> x;
};
template <typename T>
class AllTuples<T, 1>
{
Tuple<T, 1> x;
};
AllTuples<float, 10>;
I’m sorry you are out of luck. Unless you use a preprocessor metaprogramming library (boost.pp), there is no way to automate that. Not with template metaprogramming.
What you have produced there are implicit instantiations of
Tuple<float, N>. But implicit instantiations have two important differences to explicit instantiationsFor the latter, since I’m not aware of the reason for that, I will just quote the spec.
That may have to do with linker complications, because implicit instantiations for the same template can occur multiple times in multiple translation units, so they need special handling. Explicit instantiations and non-inline functions can’t.