Problem with optional arguments in C #defines

Posted by imikedaman on Stack Overflow See other posts from Stack Overflow or by imikedaman
Published on 2011-01-01T12:00:54Z Indexed on 2011/01/01 12:54 UTC
Read the original article Hit count: 155

Filed under:

Hi, I'm having a problem with optional arguments in #define statements in C, or more specifically with gcc 4.2:

bool func1(bool tmp) { return false; }
void func2(bool tmp, bool tmp2) {}
#define CALL(func, tmp, ...) func(tmp, ##__VA_ARGS__)

int main() {
   // this compiles
   CALL(func2, CALL(func1, false), false);

   // this fails with: Implicit declaration of function 'CALL'
   CALL(func2, false, CALL(func1, false));
}

That's obviously a contrived example, but does show the problem. Does anyone know how I can get the optional arguments to "resolve" correctly?


Additional information: If I remove the ## before _VA_ARGS_, and do something like this:

bool func2(bool tmp, bool tmp2) { return false; }
#define CALL(func, tmp, ...) func(tmp, __VA_ARGS__)

int main() {
   CALL(func2, false, CALL(func2, false, false));
}

That compiles, but it no longer works with zero arguments since it would resolve to func(tmp, )

© Stack Overflow or respective owner

Related posts about c