Vladimir Yanakiev
Vladimir Yanakiev

Reputation: 1300

Error in macro with __VA_OPT__ and parenthesis

I am using c++,gcc. I have code for logging with macro like this:

#define E_DEBUG(level, ...) \
    if (err_get_debug_level() >= level) \
        err_msg(ERR_DEBUG, FILELINE, __VA_OPT__)

#define ERR_DEBUG 1
#define FILELINE  __FILE__ , __LINE__

int err_get_debug_level(void);
void err_msg(int lvl, const char *path, long ln, const char *fmt, ...);


int main ( void ) {
  E_DEBUG(1,("%d",14));
}

The code give an error VA_OPT must be followed by an open parenthesis

I change the code according this Error in macro with __va_args__ and parenthesis. The code look like:

#define PASTE(...) __VA_OPT__


#define E_DEBUG(level, ...) \
    if (err_get_debug_level() >= level) \
        err_msg(ERR_DEBUG, FILELINE, PASTE __VA_OPT__)

#define ERR_DEBUG 1
#define FILELINE  __FILE__ , __LINE__

int err_get_debug_level(void);
void err_msg(int lvl, const char *path, long ln, const char *fmt, ...);


int main ( void ) {
  E_DEBUG(1,("%d",14));
}

It gives an error unterminated VA_OPT. How should I fix it?

Upvotes: 0

Views: 329

Answers (1)

Holt
Holt

Reputation: 37661

__VA_OPT__ is used to conditionally insert something in your macro, it's not equivalent to __VA_ARGS__, you need both:

#define E_DEBUG(level, ...) \
    if (err_get_debug_level() >= level) \
        err_msg(ERR_DEBUG, FILELINE __VA_OPT__(,) __VA_ARGS__)

This will not compile with your code because you used ("%d", 14) and I don't really understand why. If you remove the extra brackets, the code compiles.

Upvotes: 1

Related Questions