13

There are plenty of questions on Stack Overflow regarding the use of do { ... } while(0) in macros, but this is a bit different. I understand why do { ... } while(0) is used to wrap multiple lines of code in a macro expansion. But there is another form I often see: ({ ... }).

The ({ }) form has the advantage that it is an expression and can have a "return value". It also (subjectively) reads better than do { } while(0). So why isn't it always used? What advantage is there to using do { } while(0) in a macro instead?

Alex D
  • 29,755
  • 7
  • 80
  • 126
  • Note, in C++11 you can get a *similar* effect (as far as being an expression) with `[&](){/*...*/}()`, but it still has several disadvantages, such as being unable to `return` (need to implement rust-style `try!`) or use `decltype` on the expression (since lambdas aren't allowed in unevaluated contexts). – o11c Jun 25 '15 at 00:48

1 Answers1

31

Because ({...}) is a GCC extension.

Quentin
  • 62,093
  • 7
  • 131
  • 191