One can define a static array at compile time as follows:
const std::size_t size = 5;
unsigned int list[size] = { 1, 2, 3, 4, 5 };
Question 1 – Is it possible by using various kinds of metaprogramming techniques to assign these values "programmatically" at compile time?
Question 2 – Assuming all the values in the array are to be the same barr a few, is it possible to selectively assign values at compile time in a programmatic manner?
eg:
const std::size_t size = 7;
unsigned int list[size] = { 0, 0, 2, 3, 0, 0, 0 };
- Solutions using C++0x are welcome
- The array may be quite large, few
hundred elements long - The array for now will only consist of
POD types - It can also be assumed the size of
the array will be known beforehand,
in a static compile-time compliant
manner. - Solutions must be in C++ (no script, no macros, no pp
or code generator based solutions pls)
UPDATE: Georg Fritzsche's solution is amazing, needs a little work to get it compiling on msvc and intel compilers, but nonetheless a very interesting approach to the problem.
Best Answer
The closest you can get is using C++0x features to initialize local or member arrays of templates from a variadic template argument list.
This is of course limited by the maximum template instantiation depth and wether that actually makes a notable difference in your case would have to be measured.
Example:
Usage for your
1..5
case: