Saturday, September 15, 2007

do {} while(false) for macro

The following url say the reason.

In case a macro having multi-lines is used in if-else statement without brace (i.e. '{~}'), this do {} while is required.

Assum that there is a multi line macro as bellows,
#define A_MACRO a++; \
b++; \

The following code will make compile-error.

if( bFlag) A_MACRO;
else B_MACRO;

But the following macro does not make compile-error by do {} while(false).

#define A_MACRO do { a++; \
b++; \
c++; } while(false)

No comments: