1

I want a C macro that will expand into a function with an extra parameter based on a condition.

Something like this:

#define EXTRA 7
#ifdef ADD_ONE_MORE_ARG
#define dothis(...) dothat(EXTRA,...)
#endif

Such that dothis(5); is expanded into dothat(EXTRA, 5); but I can't remember the syntax. The answer I found here didn't help. Thanks.

Nagev
  • 10,835
  • 4
  • 58
  • 69
  • 3
    Is [this](https://stackoverflow.com/questions/679979) what you're looking for? – user3386109 Jun 05 '19 at 17:05
  • 1
    Note that if `dothat` is a function with a fixed number of parameters (say 4), then you can write the macro as `#define dothis(x,y,z) dothat(EXTRA,(x),(y),(z))` – user3386109 Jun 05 '19 at 17:22
  • Didn't see a relevant answer there @user3386109 thanks. The other function does take a variable number of arguments. – Nagev Jun 06 '19 at 11:02

1 Answers1

3
#define callx(...) call(EXTRA, __VA_ARGS__)
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • And here's the documentation which has an example that covers exactly what I was after. Didn't read the right section at first! https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html – Nagev Jun 06 '19 at 11:00