5

Possible Duplicate:
Programmatically create static arrays at compile time in C++

I have lots of data to be stored in a fixed array, with its elements depend on position. The value of each element can be calculated at compile time.

My code is almost like:

int fun(int p) // maybe constexpr
{
    return 0x1<<p;
}

int a[17] = {
    repeat_fun_from_0_to_16();
};

Since all of the value can be determined at compile time, there should be a way to do this, I guess.

I also checked out there's a repeat() in boost.assignment, but don't know how to use it with this situation.

Community
  • 1
  • 1
liuyanghejerry
  • 3,771
  • 5
  • 32
  • 38
  • 4
    This http://stackoverflow.com/questions/12108390/c11-compile-time-calculation-of-array and this http://stackoverflow.com/questions/2978259/programmatically-create-static-arrays-at-compile-time-in-c?lq=1 may help –  Dec 28 '12 at 12:02
  • Thanks, @aleguna ! I've figured out this with the second link. – liuyanghejerry Dec 28 '12 at 12:27

1 Answers1

1

Thanks to @aleguna , I've solved this problem by this answer.

All I need to change is the meta function:

template<size_t index> struct MetaFunc { 
    enum { value = index << 1 }; 
};
Community
  • 1
  • 1
liuyanghejerry
  • 3,771
  • 5
  • 32
  • 38