I have to use a huge dictionary with integer (or enum) keys and string values. But this is totally constant. No way to change in runtime. Is there a way (using templates etc.) to retrieve dictionary data at compile time instead of using existing dictionary structure?
Share
Clang and LLVM have solved your issue by generating tables containing their objects, using a combination of code generation and preprocessor trickery.
You can skip either step, depending on your own setup. For example:
Now, you can generate your enum:
In Clang and LLVM, a code generation phase is used to generate the .inc from more pleasant definition files.
It works pretty well… but do be aware that any modification of the enum implies full recompilation. You might wish to go to a “codeset” approach, where the enum is used internally but never leaked outside, and stable values (those of the enum) are provided to the client (
unsigned), so that old clients can link to the new libraries without recompilation: they will be limited to use the old set of codes, which is no problem if it’s stable.