Initialize const array at compile time using pattern length

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

Is it possible to initialize the next array at compile time?

template<int n> void foo() { static int pairs[2*n]; // = {0,0, 1,1, ..., n-1,n-1} for (int i = 0; i < n; i++) { pairs[2*i] = pairs[2*i+1] = i; } do_something_with_pairs(pairs); } 

(I use Clang on Xcode 4.5, so C ++ 11 is fine)

+6
source share
1 answer

As far as I know, it is impossible to expand the array initializer and exclude a recursive template-based solution as a means of generating static initialization data.

However, you can do the simple task of having a static array with as many data points as you ever use. This can be generated by a simple script. Or, for example, using the Boost preprocessor library.

Then you can simply use a pointer to this array.

0
source

All Articles