Nemanja Trifunovic wrote:
I have yet to see such a case, at least with C++.
What would be your preferred way for initializing parallel arrays, or arrays that have to be parallel to enumerations? Abusing the preprocessor won't always allow one to write very nice source code (sometimes it's necessary to throw in garbage identifiers merely to keep the compiler from squawking) but how would you suggest doing something like the following if as much data as possible must be in read-only storage?
#include <stdio.h>
#define my_list \
X(foo) \
X(bar) \
X(quack) \
X(moo) \
X(meow) \
X(woof) \
X(bah) \
X(oink) \
/* Do-nothing line concatenated into above definition */
/* Define enumerations en_* for each item in my_list */
#define X(y) en_##y,
enum {my_list en_COUNT};
#undef X
/* Define st[] containing a string for each item in my_list */
#define X(y) #y,
const char *st[] = {my_list 0};
#undef X
void main(void)
{
printf("\n%d %s",en_foo,st[en_foo]);
printf("\n%d %s",en_moo,st[en_moo]);
printf("\n%d %s",en_bah,st[en_bah]);
printf("\n");
}
One could include the list twice in the source code--once to define enumeration types and once to define the strings--but that would run the risk of having the two copies get out of sync. One could also use some other utility to take a list of names and output C-compilable source to implement it. If sufficient RAM were available, one could generate st at runtime. On the other hand, if the preprocessor can do what needs to be done, why bring in some other tool?