1

Damn, Xmacros in C are awesome.
Why didn't I know about this before.

Comments
  • 4
    Gesundheit.
  • 3
    Looks like a fast way to make simple things complicated. Maybe they have good uses somewhere.
  • 1
    @electrineer
    I use them to have the members of a config struct and their default values for the init function, all in one place.
  • 2
    Wait, what did I miss out on ?
  • 0
    ssssss...
  • 3
    @dder
    It's essentially

    #define X(A, B, C) BOILERPLATE
    #include “x.h“
    #undef X

    x.h contains lines like
    X(int, a)
    X(char, b)

    You move the stuff that changes into it's own section and let a short-lived macro generate the boilerplate.
    This is especially useful, when used across multiple files.

    Eg,

    --x.h--
    X(int, a, 1)
    X(int, b, 7)

    --types.h--
    typeset struct {
    #define X(A,B,X) A B;
    #include “x.h“
    #undef X
    } extype;

    --types.c--
    void exinit(extype *e) {
    #define X(A,B,C) e->A = C;
    #include “x.h“
    #undef X
    }
  • 1
    @metamourge why is this called an Xmacro? Looks just like normal macros except someone decided to call all of them X instead of something like INIT() or something. Is that it?
Add Comment