"Markus Dehmann" <ma************@gmail.com> wrote...
I have a
static const bool debug = false;
variable in my class. Debug output goes like this, trivially:
if(debug) cerr << "debug message";
Is it guaranteed that this if/then clause is always optimized away if the
debug variable is false?
Nope. Nothing is guaranteed when it comes to optimisation. Unless
you remove it yourself somehow, it may still be there.
The more generically guaranteed approach is to use a macro that would
expand to nothing if NDEBUG is defined:
#ifndef NDEBUG
#define PRINTDEBUGMESSAGE(a) cerr << (a)
#else
#define PRINTDEBUGMESSAGE(a)
#endif
Now, when you use
PRINTDEBUGMESSAGE("debug message");
in your code, it will be an empty statement if NDEBUG is defined (usually
so in release builds).
Similar approach can be taken to output more than one item at a time.
V