I have the following macro:
#define IF_TRACE_ENABLED(level) if (IsTraceEnabled(level))
The user code should look following:
IF_TRACE_ENABLED(LEVEL1)
{
... some very smart code
}
The emphasis here on curly brackets – I want to prevent “if” from macro to “eat” other code:
if (...)
IF_TRACE_ENABLED(LEVEL1)
printf(....);
else
bla bla bla
In this example IF_TRACE_ENABLED “eats” else block.
Is there way to enforce user code not compile without curly brakes or there are other to define the macro to achieve the safety?
This doesn’t force the user of the macro to use braces, but it will prevent an
elseclause from being unintentionally eaten:A side note: braces around the
printf()in the second example of the question wouldn’t have fixed the problem – theelseassociated withbla bla blawould still be bound to theifstatement in the macro.