我有一个非常基本的类,将其命名为 Basic,几乎用于一个更大项目的所有其他文件。在某些情况下,需要有调试输出,但在发布模式下,这不应该被启用并且是 NOOP。
目前在标题中有一个定义,它根据设置打开或关闭 makro。因此,当关闭时,这绝对是一个 NOOP。我想知道,如果我有以下代码,编译器(MSVS / gcc)是否能够优化函数调用,使其再次成为 NOOP。(通过这样做,开关可以在 .cpp 中,并且切换会更快,编译/链接时间明智)。
--Header--
void printDebug(const Basic* p);
class Basic {
Basic() {
simpleSetupCode;
// this should be a NOOP in release,
// but constructor could be inlined
printDebug(this);
}
};
--Source--
// PRINT_DEBUG defined somewhere else or here
#if PRINT_DEBUG
void printDebug(const Basic* p) {
// Lengthy debug print
}
#else
void printDebug(const Basic* p) {}
#endif