在libev的ev.h
中,我发现了一些看起来很奇怪的宏,看不懂:
173 # define EV_P struct ev_loop *loop /* a loop as sole parameter in a declaration */
174 # define EV_P_ EV_P, /* a loop as first of multiple parameters */
作者定义了一个宏EV_P_
作为EV_P,
,并将其作为函数定义中的第一个参数,如下所示:
int ev_run (EV_P_ int flags EV_CPP (= 0));
好奇为什么不直接写EV_P,
而不是EV_P_
,这样函数参数用逗号看起来会更清楚:
int ev_run (EV_P, int flags EV_CPP (= 0));
这是C的一个技巧还是有其他原因?对C不熟悉,之前谷歌了一下还是没有答案。
最佳答案
如果您多看 the code,您就会明白为什么。 .
#if EV_MULTIPLICITY
struct ev_loop;
# define EV_P struct ev_loop *loop
# define EV_P_ EV_P,
...
#else
# define EV_P void
# define EV_P_
...
#endif
如果 EV_MULTIPLICITY
被定义为非零,那么您将获得一个额外的参数来调用在参数列表的开头包含 EV_P_
宏。如果不是,那么您就不会。
如果宏不包含逗号,就无法删除第一个参数。
关于c - 为什么有人会为第一个参数定义宏,包括 C 中的逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783141/