我有一個系統,我可以在命令行中指定詳細級別。在我的功能,我覈對指定什麼來確定,如果我輸入一個代碼塊或不:讓預處理器處理一段代碼
#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