Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Looks like a fast way to make simple things complicated. Maybe they have good uses somewhere.
-
@electrineer
I use them to have the members of a config struct and their default values for the init function, all in one place. -
@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
} -
Hazarth94554y@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?
Damn, Xmacros in C are awesome.
Why didn't I know about this before.
rant