P99

◆ P99_FOR

#define P99_FOR (   NAME,
  N,
  OP,
  FUNC,
  ... 
)

A preprocessor pseudo iterator.

Parameters
NAMEa name or other data that is visible to all iterations
Nthe number of times the iteration is to be done
OPan operation that is performed to glue the different results of the individual iterations together
FUNCa function that is applied in each iteration

In each iteration, FUNC will be called as FUNC(NAME, X, I). Here NAME is the same as the argument to P99_FOR. I is the ordinal number of this iteration step, starting from 0. X is the Ith argument from the remaining argument list of this invocation of P99_FOR.

OP is called for iterations 1 to N - 1 to glue the results of the N iterations together. It is called in the form OP(NAME, I, REC, RES), where NAME and I are as before. REC is the result of all iterations with index less than I, RES is the result of the current iteration.

#define P00_SEP(NAME, I, REC, RES) REC; RES
#define P00_VASSIGN(NAME, X, I) X = (NAME)[I]
P99_FOR(A, 2, P00_SEP, P00_VASSIGN, toto, tutu);

Will result in

toto = (A)[0]; tutu = (A)[1];

To understand the associativity of the OP argument

#define P00_SUM(NAME, I, REC, RES) ((REC) + (RES))
#define P00_IDT(NAME, X, I) X
P99_FOR(A, 2, P00_SUM, P00_IDT, a, b, c)

Will result in

((((a) + (b))) + (c))
Warning
argument 0 may be evaluated multiple times
Remarks
argument 1 must expand to a decimal number
argument 3 should correspond to a macro name

Definition at line 84 of file p99_for.h.