It's common practice in C to use:
#define FOO() do { /* body */ } while (0)
While this is fine, it's also possible to do:
#define FOO() { /* body */ }((void)0)
{...}((void)0)
has many of the same benefits: you can't accidentally merge logic, and a ;
is required at the end of the line, so odd expressions like this don't go by un-noticed: FOO() else {...}
.
The only difference I've noticed is it means you need to use braces in if-statements.
if (a)
FOO();
else
BAR();
Must be written as:
if (a) {
FOO();
} else {
BAR();
}
Other then this quirk, it seems to work well, preventing the same kinds of problems do/while
method is typically used for.
Are there any significant differences between the 2 methods?
Said differently, if you see a code-base using {...}((void)0)
, are practical reasons to switch to using do{..}while(0)
, besides the one difference already noted?