I’d like to wrap some lines of code with some boilerplate code, so should I do it by passing multiple lines of code as a macro argument like so:
#define safeRun(x) if (ValidationOK()) {x}
int main(int argc, char **argv) {
safeRun(
foo();
bar();
)
}
Many thanks.
As written, your code will run foul of comma operators (but not, as previously claimed, commas in a function argument list).
Assuming you use C99, you can evade even that problem with variable arguments in the macro:
Now, as far as the preprocessor is concerned, there are 2 arguments to the macro, separated by the commas, but they are handled as you want. Here’s the
gcc -Eoutput:Whether what you’re proposing is a good idea is a separate discussion; these are the mechanisms that will more or less make it work.