I have a system where I specify on the command line the verbosity level. In my functions I check against what was specified to determine if I enter a code block or not:
#ifdef DEBUG
if (verbose_get_bit(verbose_level_1)) {
// arbitrary debugging/printing style code generally goes in here, e.g.:
printf("I am printing this because it was specified and I am compiling debug build\n");
}
#endif
I’d like to make this less tedious to set up, so here’s what I have so far:
// from "Verbose.h"
bool verbose_get_bit(verbose_group_name name); // verbose_group_name is an enum
#ifdef DEBUG
#define IF_VERBOSE_BIT_D(x) if (verbose_get_bit(x))
#else // not debug: desired behavior is the entire block that follows gets optimized out
#define IF_VERBOSE_BIT_D(x) if (0)
#endif // not debug
Now, I can do this:
IF_VERBOSE_BIT_D(verbose_GL_debug) {
printf("I don't want the release build to execute any of this code");
glBegin(GL_LINES);
// ... and so on
}
I like this because it looks like an if-statement, it functions as an if-statement, it’s clear that it’s a macro, and it does not get run in the release build.
I’d be reasonably sure that the code will get optimized out since it will be wrapped in a if(false) block but I would prefer it if there was some way I can get the preprocessor to actually throw the entire block away. Can it be done?
I can’t think of a way to do it without wrapping the entire block in a macro.
But this might work for your purposes:
Indeed the compiler should be able to optimize out an
if (0), but I often do something similar to this when the code inside the block will not compile at all when not in debug mode.