Solved

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

Posted on 2006-07-20
2
2,902 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

627 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