Timeline for Macro with number of arguments AND default parameter
Current License: CC BY-SA 4.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 4, 2022 at 7:42 | answer | added | KamilCuk | timeline score: 0 | |
Apr 19, 2020 at 15:42 | comment | added | galah92 | @MOehm Got it, and it actually might work for my scenario. Thanks! | |
Apr 19, 2020 at 15:22 | comment | added | M Oehm |
Well, LOG(X) expands to my_func(X, "") , anything else to my_func(X, "" "<fmt>", ...) , which will paste the empty string to your format string, see here. That will only work if your format string is a literal, but that's usually the case. Never mind. It was just a suggestion that you might have found useful.
|
|
Apr 19, 2020 at 15:14 | comment | added | galah92 |
@MOehm I can't seem to understand how that helps, seems like your solution rely on passing a pre-formatted string instead of format and additional arguments. I need something stronger then that.
|
|
Apr 19, 2020 at 15:10 | comment | added | M Oehm |
If this is a logging macro with printf -like syntax (and it looks like it) and if you are willing to accept an empty string as default instead of NULL and if you are okay with restricting the format of literal strings if it is given, you can make use of string-literal concatenation. In this special, but common case, the macro looks like #define LOG(CODE, ...) my_func(CODE, "" __VA_ARGS__) . (But the format checker will complain about empty format strings, so that may not be a good solution.)
|
|
Apr 19, 2020 at 14:55 | comment | added | galah92 |
I was afraid of that. It looks promising because I only really care about the second argument - the rest is going my_func as is.
|
|
Apr 19, 2020 at 14:53 | comment | added | John Bollinger | Then I think you're stuck. To the best of my knowledge, all potential approaches to problems of this kind have a built-in maximum on the number of arguments they can handle. You can make that maximum large (typically via large preprocessor code), but you cannot avoid it altogether. | |
Apr 19, 2020 at 14:47 | comment | added | galah92 |
No upper bound, as my_func has no upper bound.
|
|
Apr 19, 2020 at 14:45 | comment | added | John Bollinger | Do you want your macro to accept an arbitrary number of arguments, or is there some fixed upper bound on argument count? | |
Apr 19, 2020 at 14:34 | history | asked | galah92 | CC BY-SA 4.0 |