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.
the 1't question. You can do it like that.
There's a lot of things you can do with meta-programming. But first I'd like to ask: why would you want to do this in your case? I could understand if you needed to declare such an array in different places, so that it'd demand rewriting the same things multiple times. Is this your case?
By saying "define programmatically" I suggest the following:
By now we've defined all the values you wanted in the most abstract way. BTW if those values actually mean something for you - you could add it to the declaration:
Now let's breath life into the above declaration.
You can also handle the situation where most of your array entries are the same, with some perverted creativity :)
But you should always ask yourself: is this really worth it? Because, as you can see, you turn the code into a puzzle.
Just use a code generator. Build one or more templates that can generate the code you want, using a table or even math functions. Then include the file you generated in your app.
Seriously, a code generator would make your life much easier.
Sometime (not always) such array is generated from array of types. For example if you already have variadic class list (like template) and want to store encapsulated uint32_t value you can use: