In C++,
I need to defined a macro.
That macro would take as parameter a “block” of code.
Can we safely use several lines of code as parameter of a macro function?
I was asking myself if :
- is the following code valid, defined as valid by the standard, as in “cross-platform”?
- is there a better way to do the same (I can’t use template function there because I need the context).
#define MY_MACRO( expr ) DOSOMETHING( (expr) ); DOANOTHERTHING( (expr) ); // etc...
int my_function() {
int o = RandomNumber();
MY_MACRO(
int k = AFunction();
k++;
AnotherFunction( k + o ); // here I need to keep the context of the call
);
}
We can’t use functors because we need to have access to the context of the call.
We can’t use lambda (snif) because we use an old compiler that don’t provide it (and we can’t change it).
16.3/9:
So the multi-line macro invocation in general is fine. Of course if
DOSOMETHINGandDOANOTHERTHINGdon’t introduce braces for scope, then your particular example will redefinek.Edit:
The usual way is to capture whichever variables you need in the functor, just like a lambda does. The only thing a lambda can do that a functor can’t is “capture everything” without having to type it out, but whoever writes the lambda can see what variables they use, so that’s just convenience, they could type them all out if they had to. In your example:
You can also capture variables by reference (usually using a pointer as the data member rather than a reference, so that the functor can be copy-assigned).
If by “context”, you mean for example that the macro argument and/or the macro body might contain a
breakorgoto, and hence needs to be inside the lexical scope of the caller then you can’t use a functor or a lambda. For shame 😉