lundi 30 mai 2016

Is it possible to have a zero-cost assert() such that code should not have to be modified between debug and release builds?

I've noticed that some code often looks like this:

#ifdef DEBUG
assert(i == 1);
#endif //DEBUG

and that you may have several blocks of these sitting around in your raw code. Having to write out each block is tedious and messy.

Would it be plausible to have a function like this:

auto debug_assert = [](auto expr) { 
 #ifdef DEBUG
 assert(expr);
 #endif //DEBUG
};

or something like this:

#ifdef DEBUG
auto debug_assert = [](bool expr) {
  assert(expr);     
};
#else //DEBUG
void debug_assert(bool expr) {}
#endif //DEBUG

to get a zero-cost assert when the DEBUG flag is not specified? (i.e. it should have the same effect as if it was not put into the code without the lambda running, etc. and be optimized out by the g++/clang compilers).

Aucun commentaire:

Enregistrer un commentaire