Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to prevent the removal of the comma with empty __VA_ARGS__ in Visual C++?

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.

like image 250
Martin Ba Avatar asked Aug 18 '11 07:08

Martin Ba


1 Answers

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).

like image 92
Jay Freeman -saurik- Avatar answered Sep 25 '22 12:09

Jay Freeman -saurik-