I was wondering if in general the ELEM comparison macro is used, where:
(v == a || v == b)
Can be replaced by:
ELEM(v, a, b)
Of course, this can be done with a macro,
#define ELEM(v, a, b) (((v) == (a)) || ((v) == (b)))
However, then you need to define several macros with argument counting.
Using a simple python script, I came up with the following:
From this python3- script:
ELEM_TOTAL = 16 for i in range(2, ELEM_TOTAL + 1): print("#define ELEM%d(" % i, end="") print("v, ", end="") args = [chr(ord('a') + j) for j in range(i)] print(", ".join(args), end="") print(") \\\n (", end="") if i == 2: print("((v) == (a)) || ((v) == (b))", end="") else: print("ELEM%d(v, %s) || ((v) == (%s))" % (i - 1, ", ".join(args[:-1]), args[-1]), end="") print(")")
But I was wondering if there are any ELEM macros that take a variable number of arguments and are at least portable enough to work with the popular C compilers (GCC, Clang, Intel, MCVC).
c macros c-preprocessor
ideasman42 Jul 19 '14 at 4:37 2014-07-19 04:37
source share