通过 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
,则只应编译此行。这也可以通过使用这个来解决:
#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
)。
我遇到了一些问题,可能很容易解决。
我有这样的代码:
#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
但我希望在同一行中只使用一个简短的宏。
您可以像这样定义宏:
#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
)。