P99

◆ P99_VA_ARGS

#define P99_VA_ARGS (   X)    size_t X /*!< the number of arguments that follow */, ...

Helper macro to declare a variable length parameter list.

Inside the declared function X will be of type size_t and should hold the actual length of the list. It can be used as the argument to va_start.

Wrap your function in a macro that uses P99_LENGTH_VA_ARG. If used through that macro, the correct value for X will always be provided at compile time. Declare such a function as follows:

unsigned P99_FSYMB(toto)(unsigned a, P99_VA_ARGS(number));
#define toto(A, ...) P99_FSYMB(toto)(A, P99_LENGTH_VA_ARG(__VA_ARGS__))

In the definition of the function you may then use the va_start etc from stdarg.h to traverse the argument list.

unsigned P99_FSYMB(toto)(unsigned a, P99_VA_ARGS(number)) {
unsigned ret = 0;
va_list ap;
va_start(ap, number);
for (size_t i = 0; i < number; ++i) {
ret += va_arg(ap, unsigned);
}
va_end(ap);
return ret % a;
}

In this toy example toto can be used as

unsigned magic = toto(3, 1, 3, 5, 7);

which will result in converting 1, 3, 5, 7 (the variable arguments) to unsigned, computing their sum, i.e 16u, and compute that value mod 3u (the fixed argument a). So magic should hold the value 1u thereafter. (But beware of implicit integer promotion rules for integers of small width.)

Parameters
Xis the name of the ‘length’ parameter that you want to use in the definition of the function. As in the example above it should then be used as the second argument to va_start and as a loop boundary when you actual handle the argument list. X is implicitly declared to have type size_t.
See also
P99_LENGTH_ARR_ARG for a way that is generally more efficient than using va_list
P99_LENGTH_VA_ARG
P99_FSYMB

Definition at line 213 of file p99_args.h.

P99_VA_ARGS
#define P99_VA_ARGS(X)
Helper macro to declare a variable length parameter list.
Definition: p99_args.h:213
P99_FSYMB
#define P99_FSYMB(NAME)
Mangle NAME.
Definition: p99_args.h:223
i
P00_CLAUSE2 i(_Pragma("weak p00_getopt_comp"))(_Pragma("weak p00_getopt_comp