C++ 在#define宏中嵌入if语句
我在读一篇文章,其中#define macro是这样制作的:C++ 在#define宏中嵌入if语句,c++,c-preprocessor,C++,C Preprocessor,我在读一篇文章,其中#define macro是这样制作的: #define TEST(level) if (level > 2) ; else std::cout Test(1) << 3; Test(3) << 4; 然后可以在如下代码中使用: #define TEST(level) if (level > 2) ; else std::cout Test(1) << 3; Test(3) << 4; 它将以以下内容取代:
#define TEST(level) if (level > 2) ; else std::cout
Test(1) << 3;
Test(3) << 4;
然后可以在如下代码中使用:
#define TEST(level) if (level > 2) ; else std::cout
Test(1) << 3;
Test(3) << 4;
它将以以下内容取代:
std::cout
及
将替换为空字符串(在代码文件中)。然而,如果它是这样工作的,那么它应该抛出一个错误,因为
<< 3;
我相信你弄错了:
对于测试(3)2);我相信你弄错了:
对于测试(3)2);else std::cout它将测试(x)
替换为
if (x > 2)
// do nothing
else
std::cout << x
它将测试(x)
替换为
if (x > 2)
// do nothing
else
std::cout << x
预处理器是文本替换。整个if-else在中被替换。预处理器是文本替换。整个if-else在中被替换。宏中不包含True,我真傻。编辑。宏没有包含True,我真是太傻了。编辑。
if (x > 2)
// do nothing
else
std::cout << x
#define TEST(x) if (!x > 2) std::cout