Search code examples
programming-languagesmacrospreprocessor

Static data definition as language feature


I am writing a program in C++ in what I use some constant data. I build that data procedurally at startup, and never again change it. I know that ideally that data should be static data at the binaries, but that is not the case since I have to build them at first.

In C++ I could define static consts members in classes with that data or simply global consts. By doing this I would have static data in the binaries, but then I would not be able to program their contents. I would have to build them elsewhere and paste the result in the code. In my case it would be a bunch of binary data uglily and nonsenselesly encoded inside the source file.

So I started to wonder, is there any language that support such feature that enables me to define my static data procedurally, but that resolves it at compile time and embed inside the binaries? Could any kind of optimization handle these cases? If the procedure/function that generates the data takes no external parameters and has predictable results, it could be safely optimized with the results by the compiler. Does compilers take that path? Any languages you know does treat explicitly this matter?

I know of C preprocessor, but it is really not Turing-compatible, and its syntax is not as attractive as it would be a function modifier that tells it should be resolved at compile time.


Solution

  • Build a program to generate the data (you already have that), have it's output be in C++, add the generation to your Makefile, and use the generated data with #include.