NO

Author Topic: Variadic macros trailing comma  (Read 3623 times)

kankamuso

  • Guest
Variadic macros trailing comma
« on: October 31, 2007, 03:48:16 am »
Hi Pelle. Thanks a lot for Pelles C. It's a great development system.

Although C99 standard (I think) doesn' t mention this detail, could you add support for calling variadic macros with no extra arguments?

Example:

Code: [Select]
#define method(o, m, ...) o.m(o, __VA_ARGS__)
This wors fine for macro calls with 3 or more arguments:

Code: [Select]
method(table, insert, "my text");
method(calc, set, 12, 144);

Unfortunately, if no variadic args are passed, the macro doesn't work.

Code: [Select]
method(table, clear);
This produces an error: Disagreement in number of macro arguments.

Maybe it's a feature to bundle into the Microsoft extensions part of Pelles C, as VC++ 2005 supports it. And GCC does so, too.

What do you thing about it?

Thanks again.
« Last Edit: October 31, 2007, 04:07:16 am by kankamuso »

kankamuso

  • Guest
Re: Variadic macros trailing comma
« Reply #1 on: October 31, 2007, 04:05:12 am »
Rereading Pelles C documentation I've found the following (emphasis mine):

Quote
If the parameter list in the definition ends in ellipses (...), the trailing arguments in the call, including any separating comma preprocessing tokens, are merged to form a single item: the variable arguments. The predefined identifier __VA_ARGS__ is used to access this item. There must be at least one argument to match the ellipsis. This requirement avoids the problems that occur when the trailing arguments are included in a list of arguments to another macro or function. [C99]

Maybe the best solution is suggested by the GCC approach, which makes the intent explicit. From the link I posted:

Quote
The ## token paste operator has a special meaning when placed between a comma and a variable argument. If you write #define eprintf(format, …) fprintf (stderr, format, ##__VA_ARGS__) and the variable argument is left out when the eprintf macro is used, then the comma before the ## will be deleted. This does not happen (...) if the token preceding ## is anything other than a comma.


Synfire

  • Guest
Re: Variadic macros trailing comma
« Reply #2 on: October 31, 2007, 06:25:45 am »
lol, I've actually had this exact discussion (believe it or not doing the same exact #define) on this board before. In fact, it was one of my first posts: http://www.smorgasbordet.com/forum/index.php?topic=1307

As you can see from there, the decision was to stick to C99. Although I'm sure if enough people request it Pelle *might* change his mind.. I put in a vote for it. (of course)  ;D

kankamuso

  • Guest
Re: Variadic macros trailing comma
« Reply #3 on: November 01, 2007, 12:08:03 am »
Well, all my hope is gone now I know it's been already discussed. I see we'll keep using method and method1 macros, as a kludge, for a loooong time.