C 多行宏 : do/while(0) vs scope block

标签 c macros multiline

<分区>

Possible Duplicates:
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 { \
    do_stuff_here \
    do_more_stuff \
  } while (0)

与使用基本 block 相比,以这种方式编写代码有什么好处(如果有的话):

#define FOO \
  { \
    do_stuff_here \
    do_more_stuff \
  }

最佳答案

Andrey Tarasevich 提供了以下解释:

  1. On Google Groups
  2. On bytes.com

[对格式进行了细微更改。在方括号 []] 中添加括号注释。

The whole idea of using 'do/while' version is to make a macro which will expand into a regular statement, not into a compound statement. This is done in order to make the use of function-style macros uniform with the use of ordinary functions in all contexts.

Consider the following code sketch:

if (<condition>)
  foo(a);
else
  bar(a);

where foo and bar are ordinary functions. Now imagine that you'd like to replace function foo with a macro of the above nature [named CALL_FUNCS]:

if (<condition>)
  CALL_FUNCS(a);
else
  bar(a);

Now, if your macro is defined in accordance with the second approach (just { and }) the code will no longer compile, because the 'true' branch of if is now represented by a compound statement. And when you put a ; after this compound statement, you finished the whole if statement, thus orphaning the else branch (hence the compilation error).

One way to correct this problem is to remember not to put ; after macro "invocations":

if (<condition>)
  CALL_FUNCS(a)
else
  bar(a);

This will compile and work as expected, but this is not uniform. The more elegant solution is to make sure that macro expand into a regular statement, not into a compound one. One way to achieve that is to define the macro as follows:

#define CALL_FUNCS(x) \
do { \
  func1(x); \
  func2(x); \
  func3(x); \
} while (0)

Now this code:

if (<condition>)
  CALL_FUNCS(a);
else
  bar(a);

will compile without any problems.

However, note the small but important difference between my definition of CALL_FUNCS and the first version in your message. I didn't put a ; after } while (0). Putting a ; at the end of that definition would immediately defeat the entire point of using 'do/while' and make that macro pretty much equivalent to the compound-statement version.

I don't know why the author of the code you quoted in your original message put this ; after while (0). In this form both variants are equivalent. The whole idea behind using 'do/while' version is not to include this final ; into the macro (for the reasons that I explained above).

关于C 多行宏 : do/while(0) vs scope block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1067226/

相关文章:

c - 如果从标准输入读取,刷新标准输出似乎没有效果

C 字符串(字符数组): ignores next scanf because of spaces

c# - 寻找用于生成标准 PRBS 序列的*通用*算法

c - 如何刷新用 stdio 函数编写的输出?

import - 用于评估表达式并创建导入的宏

macros - 带有参数 x 和输出 $x 和 @x 的宏

android - TextView 忽略了较长文本和第二行的边界。如何解决?

c# - 添加文本时如何防止多行文本框滚动?

regex - 在 libreoffice calc 宏中使用正则表达式从单元格中的括号中提取文本