Solved

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

Posted on 2006-07-20
2
2,806 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:formula420
2 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 100 total points
ID: 17147890
While using the stringize is required for gcc, it should also work for VC, i.e. this:
      #define foo(...)   bar(x,y, ##__VAR_ARGS__)
should work in both.
0
 

Author Comment

by:formula420
ID: 17148434
Not sure why I didn't think to test that out first, but thanks =) Works fine
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 101
stack 22 166
Converting a float to a string function in c languange 10 207
Acrinis True image 2 78
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question