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