Search code examples
c++compilationconditional-compilation

Does removal of asserts when NDEBUG is defined happen before inlining of functions?


For the above example, I am trying to figure out if the increment method will actually be inlined. If the assertion will be removed by the compiler when I define the NDEBUG flag, the increment method will become a single line and thus the probability of it being actually inlined will increase. So the question comes down to the order of removing assertions and making inline decisions by the compiler.

#ifndef Example_h__
#define Example_h__
#include <cassert>

class A
{
private:
    int m_value = 0;

public:
    void increment();
};

inline void A::increment()
{
    ++m_value;
    assert(m_value < 100);
}

int main()
{
  A a;
  a.increment();
}

#endif

Solution

  • assert is a macro, so it is handled by the preprocessor before the compiler sees any of the resulting code.