6

On Visual Studio 2005 I have a macro that looks like this (examplified!!):

#define MY_CALL(FUN, ...) \
  if(prepare(x, y)) {     \
    FUN(__VA_ARGS__);     \
  }
/**/

As long as the function takes at least one argument, I'm fine.

When the function takes zero arguments, the preprocessor "helpfully" removes the "trailing comma", expanding something like this:

if(prepare(x y)) { funct(); }

Great, isn't it?

How can I fix this macro, so that it'll work with zero __VA_ARGS__ on Visual C++ (VS 2005)?


Apparently this is a bug in VS2005.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • What are `x` and `y` - are they macro arguments? If no, preprocessor shouldnt modify `prepare(x,y)` to `prepare(x y)`. If yes, where are they - you didnt mention – Ajay Aug 18 '11 at 08:20
  • @Ajay: prepare is a normal function. x y are just example arguments to this function. Nothing to do with the macro. – Martin Ba Aug 18 '11 at 08:52
  • I am wondering why preprocesor would remove the unrelated comma between x and y. I have written similar macros for variable arguments in marco, and I havent encountered this issue. – Ajay Aug 18 '11 at 09:17
  • Yes, and the macros compile for VS2008 and VS2010 also! – Ajay Aug 18 '11 at 11:49
  • @Ajay: You *do* also have `whatever(__VA_ARGS__)` and not `whatever(something, __VA_ARGS__)`, do you? – Martin Ba Aug 18 '11 at 14:05
  • 1
    No, I don't have such macro. You may refer this: http://connect.microsoft.com/VisualStudio/feedback/details/278752/comma-missing-when-using-va-args Which clearly states VS2005 has this bug even in SP1. I compiled it in VS2008 and it works. – Ajay Aug 18 '11 at 16:09

1 Answers1

5

Unfortunately, I do not use Visual C++ anymore (and as so cannot verify this works), but can you try this?

#define MY_CALL(FUN, ...) \
  if(prepare(x, y)) {     \
    int fail[] = {0,}     \
    FUN(__VA_ARGS__);     \
  }

Using gcc 4.2, both {0,} and {0} are allowed in that context, so if the comma gets deleted or not it would not matter. However, I am not certain whether that is generally accepted in the spec, a commonly implemented extension, or something specific to gcc.

If the {0,} syntax is allowed by Visual C++, then this would hopefully solve your problem (assuming I understand correctly that the most recent comma before __VA_ARGS__ is what is being incorrectly deleted, regardless of where it is appearing in the syntax).

Jay Freeman -saurik-
  • 1,759
  • 1
  • 13
  • 13