Question

For cleaner error handling I use a macro (it uses C99 and GCC extensions); the behavior is like standard assert:

#define A(cond, msg, ...) ({ \
        if (!(cond)) { \
                if (msg) \
                        say(msg, ##__VA_ARGS__); \
                else \
                        say("Error at file %s, function %s, line %u: %s", \
                                __FILE__, __func__, __LINE__, #cond); \
                A_RETURN(); \
        } \
})

where say is a formatted output. And use it like that:

#undef A_RETURN
#define A_RETURN() ({ fclose(f); free(p); return false; })

A(foo() != FOO_ERROR, 0);
A(bar() != BAR_ERROR, "bar failed");

When I don't have a specific error message, I have to write A(cond, 0). But I want just write A(cond) in this case. How to modify my A macro for this behavior? I.e. I need a way to check if msg argument isn't passed to the macro.

Was it helpful?

Solution

From the help of suggested question I came to the point that you can modify your macro like this.

#define A() .... //your macro

#define A_CALC_ARG_IMPL(_1,N,...) N
#define A_CALC_ARG(...) A_CALC_ARG_IMPL(__VA_ARGS__,0)

#define A_NEW(...) A(cond, A_CALC_ARG(__VA_RGS__))

so your new A_NEW macro call will be expanded to A(cond, 0) if you don't pass msg.

Variadic macro is explained nicely at this blog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top