honey the codewitch
honey the codewitch

Reputation: 341

Variadic Macro Expansion: Expected primary expression before ) when __VA_ARGS__ is used

I have the following code:

#define TRACE_NONE 0
#define TRACE_ERROR 1
#define TRACE_WARNING 2
#define TRACE_INFORMATIONAL 3
#define TRACE_DEBUG 4
#define TINFO(str,cat,...) trace(TRACE_INFORMATIONAL,str,cat,__VA_ARGS__)
#define TWARN(str,cat,...) trace(TRACE_WARNING,str,cat,__VA_ARGS__)
#define TERROR(str,cat,...) trace(TRACE_ERROR,str,cat,__VA_ARGS__)
#define TDEBUG(str,cat,...) trace(TRACE_DEBUG,str,cat,__VA_ARGS__)

On the #define TINFO line I'm getting error: expected primary-expression before ')' token

When I use it like this

TINFO("Session start","SES");

Edit: But if i supply a 3rd argument, like TINFO("Session start","SES","");

everything works.

I want the first variant to work

My function signature for trace is this:

void trace(int level,const char* sz,const char* cat,...);

If it matters

I don't really use __VA_ARGS__ - the last time I used it was years ago, and I just can't seem to find what I'm doing wrong here, after looking at several examples. I'm missing something easy - I know I am - but I don't know what it is for the life of me.

This code can be GCC specific.

Upvotes: 3

Views: 2258

Answers (1)

honey the codewitch
honey the codewitch

Reputation: 341

After some experimentation, I found I needed to use ##

#define TINFO(str,cat,...) trace(TRACE_INFORMATIONAL,str,cat, ##__VA_ARGS__ )
#define TWARN(str,cat,...) trace(TRACE_WARNING,str,cat, ##__VA_ARGS__  )
#define TERROR(str,cat,...) trace(TRACE_ERROR,str,cat, ##__VA_ARGS__ )
#define TDEBUG(str,cat,...) trace(TRACE_DEBUG,str,cat, ##__VA_ARGS__ )

That worked. I had tried VA_OPT but it wasn't working because apparently my toolchain uses an old compiler (I'm cross compiling for a microcontroller and i'm not used to the toolchain yet)

Upvotes: 5

Related Questions