3 ms·
I hate X-Macros, but they're very useful in some situations. We use them to generate an enum of error values and also a string table of those names. (Unlike t
by loeg 2y ago
I hate X-Macros, but they're very useful in some situations. We use them to generate an enum of error values and also a string table of those names. (Unlike two separate tables, the X macro version is guaranteed to be the exact correct length / values align with the corresponding string.)
- IshKebab 2y agoI agree. Terrible hack, but if you're in terrible hack land they're quite useful. LLVM uses them extensively.
- msarnoff 2y agoThat's been my primary usage of them. Another is to create a list of config file options (or command line arguments) like X(foo, int, "set the number of foos") X(filename, std::string, "input filename") X(verbose, bool, "verbose logging") which can then be used to (a) generate the fields of a config struct, define the mapping from string to field (using the stringifying macro operators), define what functions to use for parsing each field, create the help message, etc. Basically like `argparse` or `clap` but much hackier. As gross as they are, the ability to define one table of data that's used multiple ways in multiple places is handy.