4

I have a system with many parameter sets "macroized" (macros of the form "#define name value,value,value). I would like to pass these to a macro, but when I do I get an error.

example:

void fn(int a, int b, int c){ return; }

#define MACRO_1(a, b, c) fn(a, b, c)
#define MACRO(...) MACRO_1(__VA_ARGS__)
#define PARAM1 1
#define PARAM2 2, 2
#define PARAM3 3, 3, 3

int main(int argc, char * argv[]){
   MACRO(0,0,0);
   MACRO(PARAM1,1, 1);
   MACRO(PARAM2,2);
   MACRO(PARAM3);
   return 0;
}

in Visual C I get:

1>c:\main.c(10): warning C4003: not enough actual parameters for macro 'MACRO_1'
1>c:\main.c(10): error C2059: syntax error : ','
1>c:\main.c(11): warning C4003: not enough actual parameters for macro 'MACRO_1'
1>c:\main.c(11): error C2059: syntax error : ','
1>c:\main.c(12): warning C4003: not enough actual parameters for macro 'MACRO_1'
1>c:\main.c(12): error C2059: syntax error : ','
1>c:\main.c(13): warning C4003: not enough actual parameters for macro 'MACRO_1'
1>c:\main.c(13): error C2059: syntax error : ','
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tletnes
  • 1,958
  • 10
  • 30
  • Looks like a bug in Visual C -- `MACRO` takes any number of arguments (...), and doesn't use `#` or `##` on them, so any macros in the arguments (`PARAM1`, `PARAM2`, etc) should be expanded before the `MACRO_1` in the body gets scanned or expanded. – Chris Dodd Apr 03 '12 at 22:28

1 Answers1

6

This is a bug in the Visual C++ compiler. The compiler does not correctly expand the variadic argument pack when __VA_ARGS__ appears as an argument to another macro.

The workaround is to use an additional layer of indirection to force the variadic argument pack to be expanded before MACRO_1 is invoked:

#define MACRO_1(a, b, c) fn(a, b, c)
#define MACRO_1_(args_list) MACRO_1 args_list
#define MACRO(...) MACRO_1_((__VA_ARGS__))

There is a Microsoft Connect bug for this issue: Variadic Macro Replacement (Wayback Machine). If this issue is important to you, please upvote that bug report.

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Re the bug report link, "The content that you requested cannot be found or you do not have permission to view it." :( I just have this issue now with the November CPT of MSVC 11.0 – Cheers and hth. - Alf Nov 07 '12 at 02:05
  • @Cheersandhth.-Alf: Old bugs have been removed from Connect. I am told that this is a temporary state of affairs, but I do not know when the old bugs will be available again. I also don't know what qualifies as "old." I do know that we have a bug actively tracking this issue internally. That doesn't mean that it _will_ be fixed--I can't make any promises--but it does mean that we haven't forgotten about the issue. – James McNellis Nov 07 '12 at 02:15