用引号定义C ++预处理器宏[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我试图在C ++中定义一个宏,它在一个变量周围加上引号。

我想要做的一个简化示例是:

#define PE(x) std::cout << "x" << std::endl;

然后当我在我的代码中键入PE(hello)时,它应该打印hello;但它只是打印x

我知道如果我做到了:

#define PE(x) std::cout << x << std::endl;

然后键入PE("hello")然后它会工作,但我希望能够使用它而不使用引号。

这可能吗?

c++ macros preprocessor
1个回答
1
投票

您可以使用字符串化运算符#

#define PE(x) std::cout << #x << std::endl;

不过,我建议您从宏中删除分号。所以,

#define PE(x) std::cout << #x << std::endl
...
PE(hello);
© www.soinside.com 2019 - 2024. All rights reserved.