通过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已定义,则仅应编译此行这也可以通过使用以下方法解决:

#ifdef ALL
    foo = thisIsSomeFunc(int foo);
#endif

但是我更希望在同一行中只包含一个简短的宏。

muXXmit2X

您可以像这样定义宏:

#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)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章