Converting a macro to an inline function
- by Rob
I am using some Qt code that adds a VERIFY macro that looks something like this:
#define VERIFY(cond) \
{ \
bool ok = cond; \
Q_ASSERT(ok); \
}
The code can then use it whilst being certain the condition is actually evaluated, e.g.:
Q_ASSERT(callSomeFunction()); // callSomeFunction not evaluated in release builds!
VERIFY(callSomeFunction()); // callSomeFunction is always evaluated
Disliking macros, I would instead like to turn this into an inline function:
inline VERIFY(bool condition)
{
Q_ASSERT(condition);
}
However, in release builds I am worried that the compiler would optimise out all calls to this function (as Q_ASSERT wouldn't actually do anything.) I am I worrying unnecessarily or is this likely depending on the optimisation flags/compiler/etc.? I guess I could change it to:
inline VERIFY(bool condition)
{
condition;
Q_ASSERT(condition);
}
But, again, the compiler may be clever enough to ignore the call.
Is this inline alternative safe for both debug and release builds?