通过Makro c++从编译中排除行
Exclude line from compilation via Makro C++
我遇到了一些问题,可能很容易解决。
我有这样的代码:
#define _MG_ALL //This might be defined in some other headerfile
#ifndef _MG_ALL
#define MG_ALL <?????>
#else
#define MG_ALL <nothing>
#endif
在代码中这样使用:
ALL foo = thisIsSomeFunc(foo);
只有在定义了_ALL
时才应该编译这一行。这也可以通过使用This来解决:
#ifdef ALL
foo = thisIsSomeFunc(int foo);
#endif
但是我希望在同一行中只有一个简短的宏
你可以这样定义宏:
#ifdef _ALL
#define ALL if(1)
#else
#define ALL if(0)
#endif
当你使用它时,它会产生类似于下面的代码
ALL std::cout << "Debug Message" << std::endl;
==> if(1) std::cout << "Debug Message" << std::endl;
一个好的编译器应该识别if-statement
中的常量值,只编译正确的部分(1 ==> if part, 0 ==> nothing
)。