Get the latest tech news

My Favorite C++ Pattern: X Macros (2023)


In this post, I talk about my favorite C/C++ pattern involving macros.

Code generation here is actually split into two distinct forms of “is bla” methods: those for concrete AST nodes ( DoWhile,While) and those for abstract base classes ( Loop). Essentially, rather than adding each new operation (e.g. convert to string, compute the type, assign IDs) as methods on each AST node class, we extract this code into a per-operation visitor. So there’s a fair bit of tedious boilerplate, and more code to manually modify when adding an AST node: you have to go and adjust the Visitor class with new visit stub.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of x macros

x macros

Photo of favorite c++ pattern

favorite c++ pattern