为了更清晰的错误处理,我使用宏(它使用 C99 和 GCC 扩展);行为就像标准assert
:
#define A(cond, msg, ...) ({ \
if (!(cond)) { \
if (msg) \
say(msg, ##__VA_ARGS__); \
else \
say("Error at file %s, function %s, line %u: %s", \
__FILE__, __func__, __LINE__, #cond); \
A_RETURN(); \
} \
})
哪里say
是格式化输出。并像这样使用它:
#undef A_RETURN
#define A_RETURN() ({ fclose(f); free(p); return false; })
A(foo() != FOO_ERROR, 0);
A(bar() != BAR_ERROR, "bar failed");
当我没有特定的错误信息时,我必须写A(cond, 0)
. 但我只想写A(cond)
在这种情况下。如何A
针对这种行为修改我的宏?即我需要一种方法来检查msg
参数是否没有传递给宏。