I’m working on something that loops over a potentially multi-dimensional array and I am running into some template issues. Here is the idea of what I’m trying to do:
template<std::size_t D = 3>
class Deriv {
public:
typedef typename boost::array<double, D> results_t;
template<typename Iterator>
results_t operator()(Iterator& objIterator)
{
double u;
double um1;
results_t derivs;
for(results_t::index i = 0; i < D; ++i) {
u = *objIterator;
um1 = *(objIterator.left<i>(1));
derivs[i] = u-um1;
}
}
};
};
So, it looks a little confusing, but it’s using a custom iterator that has a left and right template method where the template parameter is the dimension to move left or right in and the argument is the offset. For example, if objIterator points to array[i][j][k], then objIterator.left<0>(1) returns an iterator to array[i-1][j][k].
Obviously what I posted doesn’t work because i is not compile time and can’t be used as the parameter for left. I feel like recursion could be used here somehow to make this happen by replacing that for loop, but I don’t know how.
Any suggestions?
Here’s another approach:
I’ve tested with VC++ 2010, but I suspect it may not compile on more conformant compilers. If you can count down from
D-1through0rather than up from0throughD-1, then the following should be okay: