4 ms·
X Macros are a classic C++ pattern. Less known than some of the more popular techniques like curiously recurring template pattern, but still quite common in lar
by jchw 2y ago
X Macros are a classic C++ pattern. Less known than some of the more popular techniques like curiously recurring template pattern, but still quite common in large codebases.
(Although... it is a neat trick, but... It is kind of mostly useful because C++ macros are not very powerful. If they were more powerful, most uses of X Macros could be replaced by just having a single macro do all of the magic.)
I recently saw something I hadn't seen before in a similar vein. There's a million different bin2c generators, and most of them will generate a declaration for you. However, I just saw a setup where the bin2c conversion just generates the actual byte array text, e.g. "0x00, 0x01, ..." so that you could #include it into whatever declaration you want. Of course, this is basically a poor man's #embed, but I found it intriguing nonetheless. (It's nice that programming languages have been adding file embedding as a first-class feature. This really simplifies a lot of stuff and removes the need to rely on more complex solutions for problems that don't really warrant them.)
- WalterBright 2y ago> X Macros are a classic C++ pattern I've seen them in 1970s assembler code.
- jchw 2y agoY'know, I've never actually seen this before, but that makes a lot of sense. They're also (of course) a common pattern in C, and probably basically any language that has a C-style preprocessor.
- kragen 2y agohttps://en.wikipedia.org/wiki/X_macro https://en.wikipedia.org/wiki/X_macro claims they go back to the 01960s, but unfortunately the reference is to a DDJ article. Is there a chance we could like run a Kickstarter to buy DDJ's archives and fix their website?
- sitkack 2y agoYou could try, I contacted the new owners awhile ago. They don't/won't even sell new copies of the archive dvd. I tried. https://news.ycombinator.com/item?id=14345682 https://news.ycombinator.com/item?id=14345682 but a kind soul responded to that request :) https://archive.org/search?query=dr+dobbs https://archive.org/search?query=dr+dobbs https://archive.org/details/DrDobbsDVD5 https://archive.org/details/DrDobbsDVD5 https://archive.org/details/DDJDVD6 https://archive.org/details/DDJDVD6 Sad part is that the DVD doesn't have issues going back to when it started as "Dr. Dobb's Journal of COMPUTER Calisthenics & Orthodontia Running Light Without Overbyte" https://archive.org/details/dr_dobbs_journal_vol_01_201710 https://archive.org/details/dr_dobbs_journal_vol_01_201710 I have a like 81-84 that I should just send to the IA. The Living Computer Museum had a small library that had the complete archive of Dr Dobbs, but who knows where it went after Paul Allen's sister closed it and sold everything off.
- rhet0rica 2y agoI regret to inform you that 01960 is an invalid octal literal.
- kragen 2y agoNot in K&R!
- gpderetta 2y ago> If they were more powerful, most uses of X Macros could be replaced by just having a single macro do all of the magic You can do exactly that, it is just very tedious without a support preprocessor library like boost.PP.
- afdbcreid 2y ago> If they were more powerful, most uses of X Macros could be replaced by just having a single macro do all of the magic While this is probably true, I once used this pattern in Rust, which has way more powerful macro system (and also a powerful generic system).