Variadic macros and commas

David Sveningsson ext@sidvind.com
Thu Mar 1 09:18:00 GMT 2007


Hi, I have a function that takes  any number of pointers and creates an
array (an array class) and puts the pointers in it. This is the prototype:

Array *ar(Object *first = NULL, ...);

It keeps adding pointers until NULL is reached, thus the argument list
must be ended with NULL. If 'first' is NULL an empty array is returned.
However too often NULL is forgotten causing nasty bugs.

So what I want to do is to create a macro that adds NULL after the
varadic arguments.

#define ar(...) real_ar( __VA_ARGS__, NULL )

The above works as long as we have arguments but using ar() expands to
real_ar( , NULL ) which is a compiler error.

Using ## with __VA_ARGS__ doesn't seem to work in this case. The manual
doesn't say this but it seems like it only works when the comma is
placed before __VA_ARGS. I got the same error at least.

So, is there a way to do this? Or maybe I am doing it all wrong, maybe
there is a better way.

-- 


//*David Sveningsson [eXt]*

Freelance coder | Game Development Student
http://sidvind.com

Thou shalt make thy program's purpose and structure clear to thy fellow man by using the One True Brace Style, even if thou likest it not, for thy creativity is better used in solving problems than in creating beautiful new impediments to understanding.



More information about the Gcc-help mailing list