c++ - Writing debug build only assertion function ignoring side-effects -
today discovered of assertion functions still exist , being called in release build. here's example of assertion function.
bool const isdebugmode() { return false; // controlled preprocessor flag. } void assertwithreason(bool const condition, std::string const reason = "") { if (isdebugmode() , not condition) { abort(); } }
i think side-effect in condition expression preventing eliminating assertion call.
for example,
assertwithreason(glgeterror() == gl_no_error);
i expected assertion call eliminated, not. because being executed before checking debug-build.
i not sure how c++ handles case, c++ strict language, doesn't seem eliminated unless put special flag. anyway, intentionally wrote assertions removed in release build.
is possible write function surely removed in release build in c++? of course can use preprocessor macro, want avoid using preprocessor macro as possible.
i using clang, , compiler specific extension (such gcc attribute) fine.
i quite using macros purpose. yes, know, macros evil, knives (used wrong) evil, come in handy if use them right.
#define my_assert(x) {\ if (is_debug() && !x) assertfailed(__file__, __line__, __function__, #x);\ } while(0);
now can show failed (my_drawfunc.cpp: 34 : my_do_draw(): assertion failed: glgeterror == gl_no_error
or that.
Comments
Post a Comment