Define C4_FOR_EACH

Define Documentation

C4_FOR_EACH(what, ...)

A preprocessor foreach. Spectacular trick taken from: http://stackoverflow.com/a/1872506/5875572 The first argument is for a macro receiving a single argument, which will be called with every subsequent argument. There is currently a limit of 32 arguments, and at least 1 must be provided.

Example:

struct Example {
    int a;
    int b;
    int c;
};
// define a one-arg macro to be called
#define PRN_STRUCT_OFFSETS(field) PRN_STRUCT_OFFSETS_(Example, field)
#define PRN_STRUCT_OFFSETS_(structure, field) printf(C4_XQUOTE(structure) ":" C4_XQUOTE(field)" - offset=%zu\n", offsetof(structure, field));

// now call the macro for a, b and c
C4_FOR_EACH(PRN_STRUCT_OFFSETS, a, b, c);