Search code examples
c++mfcassert

Would "if ... ASSERT" be removed in release build?


Sometimes I write code like

if (ptr)
    ASSERT(ptr->member);

instead of

ASSERT(!ptr || ptr->member);

because it's more straightforward IMO. Would the redundant comparison remain in the release build?


Solution

  • I'd say that depends on your compiler.

    In release mode, the ASSERT macro won't evaluate ptr->member and will resolve to a trivial expression that the compiler will optimize out, but the if statement and the associated comparison will remain as is.

    However, if the compiler is smart enough to determine that the condition does not have any side effect, it might optimize the entire if statement away. Compiling to assembly (using the /FA option) would give you a definite answer.