vendredi 1 juillet 2016

How to initialise an array of N function pointers to function templates indexed from 0..N-1?

Given a function pointer array of function_sig, I want to initialise it to a set of template function pointers that are index via a template parameter. Is this possible?

E.g.

template<int I>
void fn() { /* do something */ }

typedef void(*function_sig)();

template<int ARRAY_SIZE>
struct items
{
  static function_sig array[ARRAY_SIZE];
};

template<int ARRAY_SIZE>
function_sig items<ARRAY_SIZE>::array = { /* what do I put here? */ };

So, is there something that I can put in the initialiser list, such that items<ARRAY_SIZE>::array will be initialised to { fn<0>, fn<1>, ..., fn<ARRAY_SIZE-1> }?

NOTE: I know of a way to do this with preprocessor magic, but I'd like to try without that. Currently, I'm thinking I'll have to do away with the array and replace it with something that would act like an array, but that would lead to a search of O(N) when indexing the pseudo-array, which I'd like to not do.

Aucun commentaire:

Enregistrer un commentaire