Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-20
2
Medium Priority
?
2,932 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

730 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