2 ms·
another proposal: _If, _Ifdef, _Ifndef inside function macros for example: #ifdef SOME_CONST #define WHATEVER(w, h, a, t, e, v, e, r) \
by smlckz 7y ago
another proposal:
_If, _Ifdef, _Ifndef
inside function macros
for example:
#ifdef SOME_CONST
#define WHATEVER(w, h, a, t, e, v, e, r) \
... common part ... \
... for SOME_CONST ... \
... common part continued ...
#else
#define WHATEVER(w, h, a, t, e, v, e, r) \
... common part ... \
... when SOME_CONST not defined ... \
... common part continued ...
#endif
With _Ifdef, the above could be written like:
#define WHATEVER(w, h, a, t, e, v, e, r) \
... common part ... \
_Ifdef(SOME_CONST, \
(... for SOME_CONST ...) , \
(... when SOME_CONST is not defined ...)
) \
... common part continued ...
With these, one could also do:
#define FACTORIAL(n) _If(n == 0, 1, (n) * FACTORIAL(n))
int f = FACTORIAL(6);
turns into:
int f = (6) * (5) * (4) * (3) * (2) * (1) * 1;
That would be very useful, I think. It might help with code duplication in function macros.
Maybe _Switch/_Case thereafter.
- clktmr 7y agoWhy not write is like this: #ifdef SOME_CONST #define HAS_SOME_CONST \ ... for SOME_CONST ... #else #define HAS_SOME_CONST \ ... when SOME_CONST not defined ... #endif #define WHATEVER(w, h, a, t, e, v, e, r) \ ... common part ... \ ... HAS_SOME_CONST ... \ ... common part continued ...