assert return-value, but run either way

Viewed 473

Typically, when I erase an element from a set, I want to assert that it was actually erased: ie

assert(s.erase(e));

but then the element doesn't get erased when NDEBUG is set. But if I write

bool removed = s.erase(e);
assert(removed);

the compiler complains that 'removed' is unused when NDEBUG is set.

How can I do this right?


I ended up just creating a utility method:

inline void run_and_assert(bool b) {
    assert(b);
}

now I can say

run_and_assert(s.erase(e));

Are there any drawbacks to this? It seems simpler to me than luiscubal's solution

2 Answers
Related