I’m trying to think of a clever way (in C) to create an array of strings, along with symbolic names (enum or #define) for the array indices, in one construct for easy maintenance. Something like:
const char *strings[] = {
M(STR_YES, “yes”),
M(STR_NO, “no”),
M(STR_MAYBE, “maybe”)
};
where the result would be equivalent to:
const char *strings[] = {“yes”, “no”, “maybe”};
enum indices {STR_YES, STR_NO, STR_MAYBE};
(or #define STR_YES 0, etc)
but I’m drawing a blank for how to construct the M macro in this case.
Any clever ideas?
A technique used in the clang compiler source is to create
.deffiles that contains a list like this, which is designed like a C file and can easily be maintained without touching other code files that use it. For example:Now, what it does is including the file like this:
In your case, you could do similar. If you can live with
STR_yes,STR_no, … as enumerator names you could use the same approach like above. Otherwise, just pass the macro two things. One lowercase name and one uppercase name. Then you could stringize the one you want like above.