Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would someone define macros for first parameter including comma in C?

Tags:

c

libev

Inside ev.h of libev, I found some macros that seems weird that can't understand:

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 */

The author defines a macro EV_P_ as EV_P, and use it as first parameter in function definitions like this:

int  ev_run (EV_P_ int flags EV_CPP (= 0));

Curious about why not just write EV_P, instead of EV_P_, so the function parameters would looks more clearly with a comma:

int  ev_run (EV_P, int flags EV_CPP (= 0));

Is this a trick in C or there are any other reasons? Not familiar with C, Google it before but still have no answers.

like image 904
igonejack Avatar asked Jun 10 '18 11:06

igonejack


1 Answers

You can see why, if you look at more of 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

If EV_MULTIPLICITY is defined to be non-zero, then you get an extra argument to calls that include the EV_P_ macro at the start of the argument list. If it's not, then you don't.

If the macro didn't include the comma, there would be no way to drop this first argument.

like image 91
Paul Hankin Avatar answered Sep 20 '22 19:09

Paul Hankin