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?
In case of inline functions there’s no guarantee the function arguments are not evaluated.
Since their evaluation might have or not have side effects you will at very best have hard times maintaining the code – in cases where there is a side effect evaluation will surely take place, in other cases it will take or not take place at the discretion of the compiler. This leaves you with a program that behaves in loosely controlled manner.
So even with your dislike towards macros you should make an informed decision. Either you use a macro and then the whole construct is eliminated by the preprocessor and the parameters are not evaluated, or you use an inline function and then the compiler decides for you.