0

Given a set of template functions and classes,

template <int dim>
class A;

template <int dim>
void F();

is it possible to define a macro MULTIDIM that would do explicit instantiation of these templates for multiple values of parameter dim (e.g. dim=1 and dim=2)?

For example,

MULTIDIM(
template class A<DIM>;
template void F<DIM>();
)

is supposed to instantiate the same entities as

template class A<1>;
template void F<1>();

template class A<2>;
template void F<2>();

One way to define it could be something like

#define MULTIDIM(x) \
  constexpr int DIM = 1; x  \
  constexpr int DIM = 2; x

but obviously this is not valid since both definitions of DIM are in the same scope.

pkarnakov
  • 123
  • 1
  • 7
  • 1
    Lookup "X macro". – user202729 Jan 30 '21 at 17:01
  • Although since this is C++, there's also something like [c++ - Instantiate function for all combinations of template params, choose instantation at run time - Stack Overflow](https://stackoverflow.com/questions/35562342/instantiate-function-for-all-combinations-of-template-params-choose-instantatio) or [c++ - How can I concisely write a lot of explicit function template instantiations? - Stack Overflow](https://stackoverflow.com/questions/50338955/how-can-i-concisely-write-a-lot-of-explicit-function-template-instantiations) -- whether it's a better way is up to you. – user202729 Jan 30 '21 at 17:04
  • thanks, X macro will work – pkarnakov Jan 30 '21 at 19:36

0 Answers0