Solved

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

Posted on 2006-07-20
2
2,776 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
Comment Utility
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
Comment Utility
Not sure why I didn't think to test that out first, but thanks =) Works fine
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now