Variadic macros with 0 arguments in C99

六月ゝ 毕业季﹏ 提交于 2019-12-03 12:49:37
schot

I see two solutions to this problem. (Three if you count 'stick with gcc').

Extra special case macro

Add a new macro for when you want to print a fixed string.

#define my_errorf(str) my_error(str, NULL)

Pro: Minimum amount of extra code.
Con: It's easy to use the wrong macro (but at least you notice this at compile time).

Put fmt inside the '...'

Vararg macro's can have only __VA_ARGS__ as parameter (unlike vararg functions). So you can put the fmt argument inside the __VA_ARGS__ and change your function.

void __my_error(const char *loc, ...);
#define my_error(...) __my_error(AT, __VA_ARGS__)

Pro: One syntax/macro for all error messages.
Con: Requires rewriting of your __my_error function, which might not be possible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!