Variadic macros

T

Thomas Carter

I understand that C99 supports variadic macros. However, is it not the
case that a variadic macro defined as

#define SAMPLE_MACRO(...) Bloody-blah

must take at least one argument? I would be interested to allow for no
arguments at all. Is this possible?
 
P

pemo

Thomas said:
I understand that C99 supports variadic macros. However, is it not the
case that a variadic macro defined as

#define SAMPLE_MACRO(...) Bloody-blah

must take at least one argument? I would be interested to allow for no
arguments at all. Is this possible?

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'.

This is also how it works in gcc - which may or may not have this right
currently.

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__))

e.g.,

PRINT(); // does a \n

Expansion:

printf("%s\n", * "\0" == '\0' ? "" : "\0");

PRINT("Boo " "Hoo") // outputs Boo Hoo.

Expansion:

printf("%s\n", * "Boo " "hoo" "\0" == '\0' ? "" : "Boo " "hoo" "\0");
 
T

Thomas Carter

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'.

This is also how it works in gcc - which may or may not have this right
currently.

You are right. When I tested with my original code in gcc I got an error,
because when I invoked my macro without arguments it got expanded into a
syntactically illegal C statement.
 
S

S.Tobias

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 "")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Variadic debug macros 5
C99's variadic macros. 7
recursive variadic macros 9
Endianness macros 48
Macros 28
Variadic templates std::tuple 2
variadic function 27
Variadic function can have type safety. 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top