在宏中执行{} while(0)与({})相比有什么优势?
关于在宏中使用do { ... } while(0)
的问题,堆栈溢出有很多问题,但这有些不同.我知道为什么do { ... } while(0)
用于在宏扩展中包装多行代码.但是我经常看到另一种形式:({ ... })
.
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: ({ ... })
.
({ })
格式的优点是它是一个表达式,并且可以具有返回值". (主观地)它也比do { } while(0)
读得更好.那么为什么不总是使用它呢?代替在宏中使用do { } while(0)
有什么优势?
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?