'do {} while (0)' replacement

Matthew Woehlke mw_triad@users.sourceforge.net
Wed Oct 8 20:30:00 GMT 2008


Frank Mehnert wrote:
> Hi,
> 
> I'm searching for a replacement of
> 
>   do {} while (0)
> 
> in a macro definition.
> 
> My problem is the following:
> 
>   #define BREAK_ON_ERROR(i)   \
>     if (1)                    \
>     {                         \
>       if (i < 0)              \
>         break;                \
>     }                         \
>     else do                   \
>     {                         \
>     } while (0)               \
> 
>   int foo(void)
>   {
>     for (;;)
>     {
>       int bar = 1, buzz = 2;
>       if (bar)
>         BREAK_ON_ERROR(buzz);
>     }
>   }
> 
> gcc-4.3 will warn with 
> 
>   'warning: suggest explicit braces to avoid ambiguous `else'
> 
> Any hints for a clever replacement of 'do {} while (0)' in this
> case? Of course, enclosing the BREAK_ON_ERROR statement like
> 
>   if (bar)
>   {
>     BREAK_ON_ERROR(buzz)
>   }
> 
> is possible but not what I want :-)

Well, it's ugly, but what about just making the semicolon illegal, i.e.:
#define BREAK_ON_ERROR(i) { if (i < 0) break; }

...or you could do something ending with "(void)0", but then this would 
not work:
if (bar)
   BREAK_ON_ERROR(buzz)
else
   ...

...unless you add braces :-(.

-- 
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
-- 
Sorry, not a winner. Please try again.



More information about the Gcc-help mailing list