简体   繁体   English

在C ++中使用条件定义宏

[英]Define macros with condition in C++

I found some existing answer on SO, but I actually don't understand how does everything there works. 我找到了一些关于SO的现有答案,但实际上我不明白那里的所有内容是如何工作的。

Basically, I want to define a macro in if/switch condition 基本上,我想在if / switch条件中定义一个宏

if (condition)
{
#define SOME_MACRO
}

So, if some condition is true, there will be defined some new macro. 因此,如果满足某些条件,则将定义一些新的宏。 But, as for now, it just defines macro anyway. 但是,到目前为止,它仍然只定义了宏。 I understand that it's not really how it should be done, but there's no normal explanation to that. 我知道这不是应该怎么做,但是对此没有一般的解释。

You can't mix macros and code like that. 您不能像这样混合宏和代码。 Macros are all processed before your code is compiled, it is just a convenience feature to save typing (and make code easier to read). 宏是在编译代码之前全部处理的,它只是保存键入(使代码更易于阅读)的便利功能。

There is a macro syntax for conditions for example, you can do: 例如,有一个用于条件的宏语法,您可以执行以下操作:

#if Condition
#define X
#endif

to conditionally define a macro. 有条件地定义一个宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM