C/C++宏定义中do{…} while(0)的用途
今天在pthread_create()的manual中看到了一个宏定义的函数:
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
- 1
- 2
- 3
- 4
其中 do{ ... } while(0)
的用法让愚钝的我困扰了半天,没想到上网一查发现也有好多人和我有一样的困惑,最后在stackoverflow上找到了答案,转述如下:
使用do{...} while (0)
命令是为了保证你宏定义的函数能够在形如if...else
的语句中保持多重操作(multistatement operation)的预设语义,从而避免因为分号而被错误的解析。
举例说明:
#define FOO(x) foo(x); bar(x)
if (condition)
FOO(x);
else //此处出现语法错误
...;
- 1
- 2
- 3
- 4
- 5
可以看到,在上面的程序中,由于宏定义中的分号原因导致if...else
语句并没有按照我们设想的去执行,即使我们在宏定义加入{ }也无事于补:
#define FOO(x) { foo(x); bar(x); }
- 1
因为这个时候程序在编译器“眼中”就成了:
#define FOO(x) { foo(x); bar(x); }
if (condition)
{ foo(x); bar(x); }; // 此处出现语法错误
else
...;
- 1
- 2
- 3
- 4
- 5
显然在上述程序中第三行由于右括号左边分号的缘故导致语法错误,在这种情况下有两种方法可供选择,
其一是使用”,”来代替”;”
#define FOO(x) foo(x), bar(x)
- 1
当foo(x)和bar(x)是两个独立的结构时这种方法可以奏效,但是如果出现复杂的功能,或者需要定义变量时此方法便无法使用了,eg:
#define FOO(x) int s = 5, foo((x)+s), bar(x)
- 1
所以更一般的情况下我们会使用do ... while
来保证宏定义的函数在语法解析时保持完整:
其二,使用do … while语句
#define FOO(x) \
do { foo(x), bar(x); } while (0)
- 1
- 2
这样一来,上述的if ... else
程序段就被视为:
if (condition)
do
{
foo(x); bar(x);
}
while (0) ;
else
...;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
于是由于分号导致的语法错误就得到了解决。
ps: do { ... } while(0)
不仅可用于宏定义中,在函数体的有些地方也有诸多巧妙的用途,这些就留着日后补充了:)