A
Ark
Hello NG,
I want a macro to count the number of tokens in a comma-separated list.
I want it to yield a constant integer expression.
I do something like
#define VERBATIM(...) __VA_ARGS__ //not quite verbatim but OK
#define COUNT(...) \
VERBATIM(sizeof(struct {char VERBATIM(__VA_ARGS__);}))
#define COUNT1(...) \
offsetof(VERBATIM(struct {char VERBATIM(__VA_ARGS__), endmark;}), endmark)
[Of course, VERBATIM is used to package multi-token argument as one
macro parameter.]
The evaluation compiles fine:
const int x = COUNT(a, b, c);
However the conditional fails:
#if COUNT(a, b, c) != 3
....
Here are IAR EWARM 5.30 errors
Error[Pe193]: zero used for undefined preprocessing identifier count.c 13
Error[Pe059]: function call is not allowed in a constant expression
count.c 13
Error[Pe193]: zero used for undefined preprocessing identifier count.c 13
Error[Pe018]: expected a ")" count.c 13
And even more errors with
#if COUNT1(a, b, c) != 3
....
IOW, neither COUNT nor COUNT1 yield a constant integer expression wanted
by #if. Compiler diagnostic is less than helpful.
What am I doing wrong?
Another question:
Is there a trick to make COUNT() and/or COUNT1() yield 0?
I want a macro to count the number of tokens in a comma-separated list.
I want it to yield a constant integer expression.
I do something like
#define VERBATIM(...) __VA_ARGS__ //not quite verbatim but OK
#define COUNT(...) \
VERBATIM(sizeof(struct {char VERBATIM(__VA_ARGS__);}))
#define COUNT1(...) \
offsetof(VERBATIM(struct {char VERBATIM(__VA_ARGS__), endmark;}), endmark)
[Of course, VERBATIM is used to package multi-token argument as one
macro parameter.]
The evaluation compiles fine:
const int x = COUNT(a, b, c);
However the conditional fails:
#if COUNT(a, b, c) != 3
....
Here are IAR EWARM 5.30 errors
Error[Pe193]: zero used for undefined preprocessing identifier count.c 13
Error[Pe059]: function call is not allowed in a constant expression
count.c 13
Error[Pe193]: zero used for undefined preprocessing identifier count.c 13
Error[Pe018]: expected a ")" count.c 13
And even more errors with
#if COUNT1(a, b, c) != 3
....
IOW, neither COUNT nor COUNT1 yield a constant integer expression wanted
by #if. Compiler diagnostic is less than helpful.
What am I doing wrong?
Another question:
Is there a trick to make COUNT() and/or COUNT1() yield 0?