I am trying to run a compile-time iterator like:
meta::reverse_iterator<2, 9>::iterate(callback());
meta::reverse_iterator<4, 7>::iterate(callback());
std::cout << "-----------------" << std::endl;
meta::iterator<2, 9>::iterate(callback());
meta::iterator<4, 7>::iterate(callback());
struct callback {
template <int i>
void operator()() {
std::cout << "print !!" << i << std::endl;
}
};
and this is how I’ve written the meta-iterator:
namespace meta {
template <int Begin, int End, bool done = false>
struct reverse_iterator {
template <typename F>
static void iterate(F f) {
f.template operator()<End>();
reverse_iterator<Begin, End-1, Begin == End-1>::iterate(f);
}
};
template <int Begin, int End>
struct reverse_iterator<Begin, End, true> {
template <typename F>
static void iterate(F) {}
};
template <int Begin, int End, bool done = false>
struct iterator {
template <typename F>
static void iterate(F f) {
iterator<Begin, End - 1, Begin == End - 1>::iterate(f);
f.template operator()<End - 1>();
}
};
template <int Begin, int End>
struct iterator<Begin, End, true> {
template <typename F>
static void iterate(F) {}
};
}
Right now I iterator calls operator()<N> But I want it to be able to call any arbitrary function supplied by user with template parameter <N> (not as run-time argument) How can that be achieved ?
also boost::bind doesn’t work with it as it calls the function object of bind instead of the real function. So there should be some way to carry default parameters to the supplied functions.
AFAIK you can’t do it directly, but if you really want it you can use a
traitsclass that call member function for you, and for different member functions write different traits: