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
80
votes
7 answers

Real-world use of X-Macros

I just learned of X-Macros. What real-world uses of X-Macros have you seen? When are they the right tool for the job?
Agnius Vasiliauskas
  • 10,413
  • 5
  • 46
  • 66
11
votes
3 answers

Can I append to a preprocessor macro?

Is there any way in standard C—or with GNU extensions—to append stuff to a macro definition? E.g., given a macro defined as #define List foo bar can I append bas so that it List expands as if I’d defined it #define List foo bar bas? I was…
J. C. Salomon
  • 3,693
  • 2
  • 26
  • 36
7
votes
2 answers

Is Misra-C compatible with X-macros?

It could be argued that in many cases X-macros increase safety, because it makes easier to make sure that generated arrays are the same length for example. However, Misra C (from 2004 reference) rules seem to have a lot of preprocessor rules that…
user694733
  • 13,861
  • 1
  • 40
  • 62
6
votes
2 answers

How to eliminate a redundant macro parameter

A while ago, I wrote a set of X-macros for a largish project. I needed to maintain coherent lists of both strings and enumerated references/hash values/callback functions etc. Here is what the function callback looks like #define…
Seth
  • 2,473
  • 16
  • 15
5
votes
2 answers

Reducing code repetition in C++ (or x-treme x-macros)

I am using x-macros to reduce the amount of repetition and code duplication while implementing a Lua interface for the game Bitfighter. The following code works fine: // Fn name Valid param profiles Profile count …
Watusimoto
  • 1,637
  • 1
  • 19
  • 34
4
votes
1 answer

Is it legal to pass the macro name to an X-Macro list

It occurred to me that the following would be a preferable style of X-macro trick: #define LIST_OF_COLOURS(X) \ X(RED) \ X(GREEN) \ X(BLUE) #define LIST_OF_FRUIT(X) \ X(APPLE) \ X(ORANGE) \ X(TOMATO) Specifically, passing…
sh1
  • 3,914
  • 13
  • 28
4
votes
2 answers

How to get reflection-like functionality in C, without x-macros

Related to this question on Software Engineering about easily serializing various struct contents on demand, I found an article which uses x-macros to create struct metadata needed for "out of the box" struct serialization. I've also seen similar…
Lou
  • 3,842
  • 3
  • 25
  • 60
3
votes
2 answers

Use specific entry of an X macro

I am using X macros to generate functions setting GPIOs to 0 or 1 (I generate around 60 functions to set around 30 GPIOs). Here is an example (I have just written this example, so the syntax may be wrong): /* X(pin_name, pin_nb) */ #define…
Umaiki
  • 355
  • 2
  • 14
3
votes
4 answers

Convert endianness of integer fields in struct using macros

Consider the following struct and functions typedef struct __attribute__((__packed__)) req_file { uint32_t start_pos; uint32_t byte_count; uint16_t name_len; } req_file; void req_file_hton(req_file *d){ d->name_len = htons(d->name_len); …
samvel1024
  • 1,007
  • 4
  • 15
  • 34
3
votes
2 answers

Using a macro as an argument in an x-macro definition

Consider the following user-style x-macro: #define PRIMES_X(func) \ func(2) \ func(3) \ func(5) We can use this to call a passed-in macro func repeatedly with first three primes. For example: #define MAKE_FUNC(num) void foo ##…
BeeOnRope
  • 51,419
  • 13
  • 149
  • 309
3
votes
1 answer

Where do my commas disappear in variadic macro expansion?

I am writing a x-macro based register file layout description system for a project of mine. Most of the time, the macros expand to a hierarchy of template classes. However, I also want an enumeration of all the registers, like this: #define…
3
votes
2 answers

X-macro enum based on previous value

I want to generate a enum with a X-Macro. The enum has to increace based on prev size. I have this #define LIST VAR(one, 0x02) VAR(two, 0x02) VAR(tree, 0x03) and want to generate this enum{ one = 0x02 + 0, two = 0x02 + one, tree = 0x03 + two } but…
DJJo14
  • 49
  • 1
  • 3
2
votes
4 answers

Creating a related x-macro from an existing one

Consider the following user-style x-macro: #define PRIMES_X(func) \ func(2) \ func(3) \ func(5) \ func(7) We can use this to expand a passed-in macro func repeatedly with the first four primes. For example: #define MAKE_FUNC(num) void foo…
BeeOnRope
  • 51,419
  • 13
  • 149
  • 309
2
votes
1 answer

X-Macros with Boost.Preprocessor?

Splitting this off from my question regarding appending to CPP macros: Has anyone here used the Boost.Preprocessor library’s data types to implement something like the X-macro?
J. C. Salomon
  • 3,693
  • 2
  • 26
  • 36
2
votes
0 answers

Is it possible to (recursively) "introspect" nested C structs using x-macros?

I was reading this article (Struct iteration through (ab)use of the preprocessor), where the author uses x-macros and offsetof to add metadata to structs which would allow their members to be easily serialized, accessed by name, etc. But it's only…
Lou
  • 3,842
  • 3
  • 25
  • 60
1
2 3 4