5 ms·
Preprocessor Abuse and Optional Parentheses
- DonHopkins 12y agoAn old Macrology trick for passing expressions with commas and all kinds of other punctuation to macros is to define the gobbledygook as one macro, and then pass its name to the other macro as a parameter.
- JoshTriplett 12y agoThat changes what the call-site of the latter macro looks like. Which would you rather write, the indirect macro usage: #define param foo, bar: baz do_stuff_with(param, other things); #undef param or the parenthesized parameter: do_stuff_with((foo, bar: baz), other things); On the other hand, passing the name of a macro lets you do even more arcane things, like passing a macro that in turn accepts arguments, and having the macro you pass it to call your macro.
- mikeash 12y agoAnother way is to just use a macro that returns its argument: #define WRAP(...) __VA_ARGS__ SOME_MACRO(WRAP(a, b, c), d) But I wanted something that let me just wrap the thing in parentheses without anything extra. Foolhardy, since it was a bunch of work and thought for an extremely small gain, but at least I got a fun article out of it.
- cardiffspaceman 12y agoI feel like I just read a subliminal promotion for the lambda calculus.
- mikeash 12y agoNot my intent, but maybe I wrote it that way subliminally too.
- deleted 12y ago[deleted]