'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?



Solution 1:[1]

__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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Holt