Quantcast
Channel: Programmatically create static arrays at compile time in C++ - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Programmatically create static arrays at compile time in C++

$
0
0

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 };
  1. Solutions using C++0x are welcome
  2. The array may be quite large, fewhundred elements long
  3. The array for now will only consist ofPOD types
  4. It can also be assumed the size ofthe array will be known beforehand,in a static compile-time compliantmanner.
  5. Solutions must be in C++(no script, no macros, no ppor 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.


Viewing all articles
Browse latest Browse all 15

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>