c - 功能类似于宏花括号或 do..while

标签 c macros

<分区>

Possible Duplicate:
Do-While and if-else statements in C/C++ macros

gcc (GCC) 4.7.2
c89

你好,

我有以下类似函数的宏,只是想知道跨多行使用时的首选用法是什么。最好使用花括号或 do..while(0) 循环。

通常我对所有事情都使用 do..while(0) 。但是我看到一些项目只使用花括号,我不确定哪个更好。

做..同时

#define DSO_ERROR(msg, res_handle_module, mem_pool, size)   do {        \
        char *dso_error = apr_palloc((apr_pool_t*)mem_pool, size);      \
        apr_dso_error((apr_dso_handle_t*)res_handle_module, (char*)dso_error, (apr_size_t)size); \
        LOG_ERR("%s dso error %s", (char*)msg, dso_error);              \
        goto dso_failure;                                               \
    } while(0); 

花括号

#define DSO_ERROR(msg, res_handle_module, mem_pool, size) {             \
        char *dso_error = apr_palloc((apr_pool_t*)mem_pool, size);      \
        apr_dso_error((apr_dso_handle_t*)res_handle_module, (char*)dso_error, (apr_size_t)size); \
        LOG_ERR("%s dso error %s", (char*)msg, dso_error);              \
        goto dso_failure;                                               \
    }

唯一的区别是分号将被预设在 do..while 循环中,而不是花括号中。

非常感谢您的任何建议,

最佳答案

花括号版本会像这样破坏用法:

if( foo )
  DSO_ERROR("Foo occured!", my_module, the_pool, 4711);
else
  printf("All is well, there is no foo\n");

这正是 do ... while(0) 构造的原因。所以这似乎值得避免。

关于c - 功能类似于宏花括号或 do..while,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13759953/

相关文章:

C宏生成printf格式字符串

泛型类的 scala 宏泛型字段不适用类泛型类型参数

c - 用于演示矩阵的简单 C 图形库

C 我的矩阵阅读器怎么了?

c - 进程退出前打印退出状态

c++ - 什么是 C++ 03 宏?

c -/usr/bin/ld : cannot find -lgcc - Error in the linkage of assembly

c - 如何从关键字 Zig 引用 C 符号?

c - nasm,宏打印未按预期工作

c++ - C++ 中的宏会提高性能吗?