3 ms·
Can I use this technique to expand MACRO(a,b,c,…) into something like F(a,b,c…); G(a,b,c…)?
by hyperhello 11mo ago
Can I use this technique to expand MACRO(a,b,c,…) into something like F(a,b,c…); G(a,b,c…)?
- viega 11mo agoThat's just: ``` #define MACRO(...) F(__VA_ARGS__); G(__VA_ARGS__) ``` The technique in the article is more often used to type check the individual parameters, or wrap a function call around them individually, etc.
- hyperhello 11mo agoOk. How about into F(a,”a”);F(b,”b”);etc. The problem being automating enums and their names in one call. Like MACRO(a,b,c) and getting a map from a to “a”.
- viega 11mo ago100%, that's definitely easy to do once you understand the technique.
- hyperhello 11mo agoPlease?
- viega 11mo agoI'm on my phone, but if you start with the top 8 lines in the code box under the ascii art, you'll get an implementation of `H4X0R_MAP()`; the bottom two lines are an example, and you can just write yourself a body that produces one term. Only thing you need to know beyond that is the stringify operator.
- viega 11mo agoAnd I should say, if you want to apply the same transformation to arguments twice, but call F() separately from G() per your starting example, you'd just apply your map twice in your top-level macro.
- WalterBright 11mo ago> if you want to apply the same transformation to arguments twice, but call F() > separately from G() per your starting example, you'd just apply your map twice in your top-level macro My brain just blew a fuse.
- viega 11mo agoOkay, finally found some time to provide you with a fully annotated example of your original ask here, assuming you wanted to transform the arguments passed to F into IDs, and the arguments passed to G into strings (as seemed to be the case from the rest of the thread). https://c.godbolt.org/z/6zqx1dsn3 https://c.godbolt.org/z/6zqx1dsn3 I've fully annotated it, so it might seem like more than it is. About half the macro code is from the original article (the chunk at the top). And I do implement both transforms for you. Each one I think is only 6 lines of code by itself, despite the rediculous amount of exposition in the comments. If you have any questions about it, let me know.
- deleted 11mo ago[deleted]