I have a function mainFunc which needs to call a several times another function process. A process has a lot of arguments but mainfunc in different calls change only two of them/ Instead of another it passes local variables which are defined and assigned before this calls to process. So I wrote a simple macro which substitutes local variables in calls to process:
#define DO_PROCESS(pred1, est1) \
do \
{ \
process(pred1, est1, arg1, arg2, arg3); \
++id; \
delete est1; \
} while(0)
arg1, arg2, arg3 are local variables in mainFunc, so I hope my macro will just use them. In mainFunc:
int arg1, arg2, arg3;
arg1 = AssignFirst();
...
Pred* pred;
Est* est;
int estArg;
int predArg;
pred = new Pred(predArg);
DO_PROCESS(pred, new Est(estArg));
delete pred;
pred = new Pred(predArg2);
DO_PROCESS(pred, new Est(estArg2));
delete pred;
pred = new Pred(predArg3);
DO_PROCESS(pred, new Est(estArg3));
delete pred;
....
However I get C2059 and C2143 errors pointing to closing curly brace and semicolon around it respectively in last line of macro.
What’s wrong with it??
The most likely cause is that there’s extra whitespace after one of the backslashes in the macro definition. Make sure that
\really is the last character on a line.Oh, and your code will leak memory, since
est1is evaluated twice in your macro definition – it expands toprocess(pred1, new Est(...), ...)and then laterdelete new Est(...), which is not what you meant. Add something likeEst *e = est1;before theprocesscall and replace the remaining occurences ofest1in your macro witheto avoid this problem.