This item in the C++ faq:
http://www.parashift.com/c++-faq-lit....html#faq-38.5
discusses macros with multiple statements.
The problem is that a macro such as
#define MYMACRO(a,b) stmt1; stmt2;
will cause unwanted astonishment if you say:
while(whatever)
MYMACRO(foo,bar);
This is also problematic:
#define MYMACRO(a,b) {stmt1; stmt2;}
because this won't compile:
if(foo())
MYMACRO(i,j);
else
baz();
since you can't have "}; else"
so the faq proposes this solution:
-------------
#define MYMACRO(a, b) \
if (true) { \
statement1; \
statement2; \
... \
statementN; \
} else
-------------
But this seems like a bad solution; what if you write this:
MYMACRO(foo,bar) // note no semicolon
baz();
it compiles fine, but baz() *never* gets called!
Is there a way to avoid this? Maybe the do/while idiom should be used
dispite the inlining problems?
// compiler may not inline functions that use this because of the loop...
#define MYMACRO(a, b) \
do { \
statement1; \
statement2; \
... \
statementN; \
} while (false)
thanks,
Erik