Preprocessor-Direktiven im Inneren eines Makros

Tilman Schmidt <[email protected]>
Newsgroups gmane.linux.suse.programming
Organization Phoenix Software GmbH
Message-ID <[email protected]>
Ich würde gerne ein Makro bauen, das

TABLE_ENTRY(FOO)

zu

#ifdef N_FOO
{ "FOO", N_FOO },
#endif

expandiert - also einen Tabelleneintrag generiert, falls
das Preprocessor-Symbol N_xyz definiert ist. Ziel ist,
dass ich nachher im Programm schreiben kann:

static const struct { const char *s; int v; } table[] = {
TABLE_ENTRY(FOO)
TABLE_ENTRY(BAR)
TABLE_ENTRY(BAZ)
TABLE_ENTRY(TOTO)
TABLE_ENTRY(TITI)
TABLE_ENTRY(TATA)
};

und table[] dann genau die Einträge enthält, für die der
entsprechende Wert auch definiert ist.

Die Expansion
  TABLE_ENTRY(FOO) => { "FOO", N_FOO },
ist ja kein Problem, aber die Abfrage, ob N_FOO definiert
ist, also das #ifdef - #endif außenrum, will nicht klappen.
Wenn ich so etwas wie

#define TABLE_ENTRY(x) \
#ifdef N_##x \
{ #x, N_##x }, \
#endif

versuche, beschwert sich CPP:

t.c:2:3: error: '#' is not followed by a macro parameter

Ideen?

TIA
T.

-- 
Tilman Schmidt
Phoenix Software GmbH
Bonn, Germany
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.