Let's say I want to handle square mesh grids of N_DIMS dimensions, each of N_RES elements length, which turns out in N_ELEMS = std::pow(N_RES, N_DIMS) elements.
The implementation I had to follow is a generalization for non-squared grids
template<typename T, size_t... DIMS>
class MeshGrid
{
// ... etc etc
}
Such as a possible instance with 3 dimensions of respectively 4, 5, 6 elements could be
MeshGrid<float, 4, 5, 6> mg; // call A
Now I like to adapt it to something like
template<typename T, size_t RES, size_t... DIMS>
class MeshSquare
{
// ... etc etc
}
Preserving the MeshGrid inner DIMS logic, in order to do run-time calls such as B
int res = 4, dims = 2
MeshSquare<float, res, ??dims??> // call B
For an example square mesh of 2 dimensions, 4 elements each = 16 total elements.
I'm honestly suspicious about what I'd like to do; I have the feeling that variadic lists must be handled at compilation time; the B call above is a nonsense.
If it could ever be possible, my question is how to expand dims to fit B call.
If you need MeshSquare to be templatized on dims, then the only thing you can do here is to generate all the instantiations in advance and then switch at run-time. E.g.
switch(dims)
{
case 0: foo<MeshSquare<float, res, 0>>(); break;
case 1: foo<MeshSquare<float, res, 0, 1>>(); break;
case 2: foo<MeshSquare<float, res, 0, 1, 2>>(); break;
case 3: foo<MeshSquare<float, res, 0, 1, 2, 3>>(); break;
case 4: foo<MeshSquare<float, res, 0, 1, 2, 3, 4>>(); break;
// ...
}
You can easily generate these kind of switches with C++17 fold expressions, with compile-time recursion and index_sequence, and many other techniques. Alternatively, consider using a library that acts as a bridge between the run-time and compile-time world such as petra (C++17).
A more appropriate solution might be simply not storing the dimensions as a template parameter, but use a more dynamic (run-time) data structure instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With