Questions tagged [x-macros]

Using macros to use the same tokens in multiple ways.

X-Macros are a use of the to use the same tokens in different patterns.
They either use #includes or additional macros for the input to transform:

#define MAPPING X(0, 55 do_a) X(1, 42, do_b) X(23, 7, do_c)

// Sometime later:
    switch(expr) {
        #define X(a, b, c) case a: return b + c(a, b);
        MAPPING
        #undef X
    default:
        abort();
    }

Or maybe:

    switch(expr) {
        #define X(a, b, c) case a: return b + c(a, b);
        #include "mapping.inc"
        #undef X
    default:
        abort();
    }

Why use X?
It's extremely short, and unlikely to already be #define-d.

46 questions
-3
votes
1 answer

C++ MACROS: conditional code include in expression NOT statement

Ok I know macros are evil and should be avoided at all costs. I am trying to reduce significant boiler plate code and repetition that will be difficult to not commit typo mistakes if I can't macrotize it. This is a somewhat contrived example as I…
Scott Idler
  • 167
  • 1
  • 10
1 2 3
4