pemo said:
From Steele/Harbison
'When such a macro is invoked, there must be as many actual arguments as
there are identifiers in /indentier_list/'
#define name(/indentier_list/, ...) /sequence-of-tokens/
From that I would infer from that, there may indeed be zero actual
parameters passed to 'name'.
All true.
This is also how it works in gcc - which may or may not have this right
currently.
IIRC, gcc is different - it removes comma before __VA_ARGS__ if it is
empty, which the Standard specification doesn't. For this reason
it is advised to skip the last mandatory parameter:
int fprintf(FILE *stream, const char *format, ...);
#define FPRINTF(stream, ...) fprintf(stream, __VA_ARGS__) //dropped `format'
(I'm writing from memory, if I'm wrong I hope someone will correct me.)
However, why would you want this? Unless you wanted to do something like?
#define mkstr(a) a "\0"
#define PRINT(...) printf("%s\n", * mkstr(__VA_ARGS__) == '\0' ? "" :
mkstr(__VA_ARGS__))
Why not simply:
#define PRINT(...) printf("%s\n", __VA_ARGS__ "")
or even:
#define PRINT(...) puts(__VA_ARGS__ "")
?
PRINT(); // does a \n ....
PRINT("Boo " "Hoo") // outputs Boo Hoo.
and
PRINT("Boo ", "Hoo") // outputs diagnostic (by your definition)
I'm not quite sure (no time to check, I once knew but forgot; again
someone please check this), I believe in C99 (in C90 it is UB) macro
arguments may be empty, so perhaps this version might be better:
#define PRINT(slit_opt) puts(slit_opt "")