[m-dev.] do {} while(0) pattern in macros

Peter Wang novalazy at gmail.com
Fri Apr 27 10:22:19 AEST 2018


On Fri, 27 Apr 2018 09:46:28 +1000, Paul Bone <paul at bone.id.au> wrote:
> 
> 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);                       \
> 		}

Hi Paul,

Now you've got two statements.

https://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html

Peter


More information about the developers mailing list