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

In Visual Studio 2005, I have a macro that looks like this (example!):

#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 a function takes null arguments, the preprocessor "helps" removes the "trailing comma", extending something like this:

 if(prepare(xy)) { funct(); } 

Great, right?

How can I fix this macro to work with __VA_ARGS__ in Visual C ++ (VS 2005)?


Apparently this is a bug in VS2005 .

+7
source share
1 answer

Unfortunately, I no longer use Visual C ++ (and so I can’t check if 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} allowed in this context, so if the comma is removed or not, it does not matter. However, I'm not sure if this is generally accepted in a specification, a commonly implemented extension, or something specific to gcc.

If the syntax {0,} allowed by Visual C ++, then this, hopefully, will solve your problem (if I understand correctly that the very last comma before __VA_ARGS__ is what is incorrectly deleted, regardless of where it appears in the syntax) .

+5
source

All Articles