lundi 4 décembre 2017

How to generate static array with running count at compile time - C++11?

I need to create a static array with running count calculated at runtime.

Eg:
int array[] = {3 = 3+0, 5 = 3+2, 6 = 5+1 ..};

I want to be able to define this array as - {3, 2, 1} and still get {3, 5, 6}

I am trying to do this using constexpr and macro, something like

constexpr get_running_count(index) { /*return the running count by going through the array*/ }
#define DEF(index, count) {count + get_running_count(index)}

array[] = {DEF(0, 3), DEF(2)...}

But it doesn't work as get_running_count has to go through the array, but I can't forward declare array, as its length is not known before hand. Is there any solution using meta programming?

Aucun commentaire:

Enregistrer un commentaire