【发布时间】:2010-11-07 05:21:00
【问题描述】:
可能的重复:
What’s the use of do while(0) when we define a macro?
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
do { … } while (0) what is it good for?
我见过一些包含在 do/while(0) 循环中的多行 C 宏,例如:
#define FOO \ 做 { \ do_stuff_here \ 做更多的东西\ } 而 (0)与使用基本块相比,以这种方式编写代码有哪些好处(如果有的话):
#define FOO \ { \ do_stuff_here \ 做更多的东西\ }【问题讨论】:
-
实际上还有另一种方法可以让事情变得正确。 ({...}) 可以做与 {} while(0) 相同的事情。参考:bruceblinn.com/linuxinfo/DoWhile.html
-
@wuxb:
({...})构造是 GCC 扩展,不受 ISO 标准 C 支持。 -
这也是 CERT 推荐的方法:wiki.sei.cmu.edu/confluence/display/c/…