for(unsigned int i = 0; i < x.size(); i++)
    assert(x[i] > 0);
When not debugging (NDEBUG flag), the resultant is an empty for loop. Is there a clean way to handle this (not executing the empty for loop); preferably without preprocessor directive, since it would defeat the purpose of assert in the first place.
inline bool all_positive(std::vector<int> const &x)
{
    for (size_t i = 0; i < x.size(); i++)
        if (x[i] <= 0)
            return false;
    return true;
}
then
assert(all_positive(x));
(although this may get you an "unused function" warning when NDEBUG is defined).
A good optimizer should be able to eliminate the entire loop when NDEBUG is defined (I've just tested mine, and it does do that).
Alternatively, you could surround the entire loop with #ifndef NDEBUG / #endif. You say this "would defeat the purpose of assert in the first place", but I don't really follow the reasoning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With