Converting a macro to an inline function

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2010-06-16T13:13:30Z Indexed on 2010/06/16 13:32 UTC
Read the original article Hit count: 137

Filed under:

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?

© Stack Overflow or respective owner

Related posts about c++