[m-dev.] do {} while(0) pattern in macros
Paul Bone
paul at bone.id.au
Fri Apr 27 09:46:28 AEST 2018
Hi Mercury team.
I remember learning the pattern:
#define PZ_WRITE_INSTR_1(code, w1, tok) \
do { \
if (opcode == (code) && width1 == (w1)) { \
token = (tok); \
goto write_opcode; \
} \
} while (0)
For macros that expand to statements while working on Mercury. While I
think that I once knew why we did this, I have now forgotten and would like
to re-learn.
Can someone remind me why we do this? My guess it's to make sure that the
macro is always a single statement and cannot be used as an expression or
expand to multiple statements in a misleading way such as in:
if (cond)
CALL_MY_MACRO(a, b);
If that's the case, why don't we just put them inside some curly brackets.
#define MAYBE_A_SAFE_MACRO(a, b, c) \
{ \
a_statement(a, b); \
another_statement(c); \
}
Thanks.
--
Paul Bone
http://paul.bone.id.au
More information about the developers
mailing list