我有一个系统,我可以在命令行中指定详细级别。在我的功能,我核对指定什么来确定,如果我输入一个代码块或不:让预处理器处理一段代码
#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
我想使这个成立还不繁琐,所以这里是我到目前为止有:
// 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
现在,我可以这样做:
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
}
我喜欢这个,因为它看起来像一个if语句,它被用作一个if语句,很明显,这是一个宏,它不在发布版本中运行。
我会合理地确信代码将被优化,因为它将被封装在if(false)
块中,但如果有某种方式我可以让预处理器实际上将整个块扔掉,我宁愿它。可以做到吗?
请清楚声明“verbose_get_bit”之外的语句(如果有的话)可能会有所不同。 – outis 2011-12-30 00:52:15
@ BrianTompsett-汤莱恩:这不适用于C++吗? – usr2564301 2016-04-05 20:21:33