Link to home
Start Free TrialLog in
Avatar of formula420
formula420

asked on

How to make macros that expand to other special macros (##__VA_ARGS__)

I have some code that I share between visual c++ 2005 and gcc. Within the code, there are a few variadic macros. The problem I have is in the different way gcc and vs handle variadic macro argument lists. VS correctly formats the argument list automatically (inserting leading commas when necessary) by simply using __VA_ARGS__. In gcc, however, ##__VA_ARGS__ is required if a leading comma is needed.

To deal with this, I was just going to write something like this

#ifdef WIN32
#define VA_ARGS __VA_ARGS__
#else
#define VA_ARGS ##__VA_ARGS__
#endif /* WIN32*/

When I compile the code in VS, it seems to work fine. However, gcc  gives these errors

/mnt/backupdrive1/Programming/pmc_libs/pmc.c:29:17: '##' cannot appear at either end of a macro expansion
/mnt/backupdrive1/Programming/pmc_libs/pmc.c:29:19: warning: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro

How can I get the desired result? I appreciate any help
ASKER CERTIFIED SOLUTION
Avatar of bpmurray
bpmurray
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of formula420
formula420

ASKER

Not sure why I didn't think to test that out first, but thanks =) Works fine