Well, you cannot use #if conditions in a macro replacement. You could use indirection, though:
#define CFG_USED(type, name, stuff) \ { .compFunc = compare_ ## type; /* */ }, #define CFG_UNUSED(type, name, stuff) /* unused */ #define CFG_ADD(type, name, stuff) CFG_USE_ ## type (type,name,stuff) #ifdef USING_int #define CFG_USE_int CFG_USED #else #define CFG_USE_int CFG_UNUSED #endif ...
Regarding warnings: you can suppress unused warnings on individual basis with an __attribute__((__unused__)).
The library approach suggested by roboticus is a good one. Let the linker work it out where possible. However, some types may not be available on all platforms, etc., so this is not always an option.
But then, there are more options. You could define the compare functions via inline versions:
There should be no warnings for unused inline functions. After that, you might use the same configure mechanism to conditionally generate function definitions:static inline int _inline_cmpfunc_int(void *a, void *b) { int _a = *(int*)a, _b = *(int*)b; return (_a > _b) - (_a < _b); }
Tricks like #include "definitions.h" multiple times, are sometimes used.static foo compare_foo(void *a, void *b) { return _inline_cmpfunc_foo(a, b); }
In reply to Re: [OT] Abusing the C preprocessor
by Anonymous Monk
in thread [OT] Abusing the C preprocessor
by afoken
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |