Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize a fixed size array and assign elements with a constexpr function in C++11 or with help of boost [duplicate]

Tags:

c++

c++11

boost

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.

like image 855
liuyanghejerry Avatar asked Nov 13 '22 14:11

liuyanghejerry


1 Answers

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 }; 
};
like image 80
liuyanghejerry Avatar answered Nov 15 '22 04:11

liuyanghejerry