Looking for a good explanation of the table generation macro idiom

Posted by detly on Stack Overflow See other posts from Stack Overflow or by detly
Published on 2010-05-28T07:15:26Z Indexed on 2010/05/28 7:21 UTC
Read the original article Hit count: 241

Filed under:
|
|

I want to make this clear up front : I know how this trick works, what I want is a link to a clear explanation to share with others.

One of the answers to a C macro question talks about the "X macro" or "not yet defined macro" idiom. This involves defining something like:

#define MAGIC_LIST \
    X(name_1, default_1) \
    X(name_2, default_2) \
    ...

Then to create, say, an array of values with named indices you do:

typedef enum {
#define X(name, val) name,

    MAGIC_LIST

#undef X
} NamedDefaults;

You can repeat the procedure with a different #define for X() to create an array of values, and maybe debugging strings, etc.

I'd like a link to a clear explanation of how this works, pitched at someone who is passably familiar with C. I have no idea what everyone usually calls this pattern, though, so my attempts to search the web for it have failed thus far.

(If there is such an explanation on SO, that'd be fine...)

© Stack Overflow or respective owner

Related posts about c

    Related posts about preprocessor