Using C X macros in combination with #ifdef -
assuming code looks in following snippet:
#ifdef cond1 extern int func1(void); #endif ... #ifdef condn extern int funcn(void); #endif my_struct funcs[] = { #ifdef cond1 {"func1 description", func1}, #endif ... #ifdef condn {"funcn description", funcn}, #endif {null, null}, };
is possible replace x macros in order minimize repetition of function names , conditions in both parts?
without #ifdef condx
conditions, appears quite straight forward. however, have no idea how include them in x macro, because not allowed use #ifdef
in #define
.
not sure whether x macros solution here. can, however, use bit of preprocessor magic reduce typing. problem remains conditional compilation (#ifdef
s) in example. without knowing these conditions difficult reduce amount of typing further.
condider following:
#define d(n) extern int func ## n(void); #define a(n) {"func" #n " description", func ## n}, #ifdef cond1 d(1) #endif #ifdef cond2 d(2) #endif my_struct funcs[] = { #ifdef cond1 a(1) #endif #ifdef cond2 a(2) #endif };
this is, think, step in direction aiming for. see does, can try
gcc -e -dcond1 <file-with-contents-above>.c
(if on unix) or
cl -e -dcond1 <file-with-contents-above>.c
(if on windows using visual studio).
Comments
Post a Comment