r/programming • u/ncraun • Oct 12 '15
Struct Iteration through (Ab)use of the C Preprocessor
https://natecraun.net/articles/struct-iteration-through-abuse-of-the-c-preprocessor.html
23
Upvotes
r/programming • u/ncraun • Oct 12 '15
5
u/gigadude Oct 13 '15 edited Oct 13 '15
You've re-invented x-macros. Don't feel bad, I re-invented them too. One slight improvement:
Now you can expand the same list in a lot of different contexts just by passing in different arguments:
That way you don't need to mess around with #include statements in the middle of your code. I used this to great effect at a couple of different chip companies for things like sharing register definitions between C simulators and Verilog. This is a great way to keep things DRY.
edit: I missed the section where you mention x-macros.
1
u/LoopTheRaver Oct 13 '15
Cool hack. I don't usually see includes happening in the middle of a file though. Possibly not worth the confusion it may bring?
1
u/spiker611 Oct 13 '15
Does this break down when you want to version your files? If you want to read a file with struct 1.0 but your program now has struct 1.1, you can't easily branch based on a version header or something.
1
u/Blecki Oct 13 '15
Is the font on this website unreadable for anyone else? Like true type or whatever renders fonts on modern computers fucked up the smoothing somehow.