布尔运算符可以与预处理器一起使用吗?

时间:2022-02-07 22:55:10

I wondering if it possible to have a preprocessor OR or AND statement? I have this code where I want to run under _DEBUG or _UNIT_TEST tags(?).

我想知道是否可以有一个预处理器或声明?我有在_DEBUG或_UNIT_TEST tags(?)下运行的代码。

What I want is something like the following:

我想要的是如下内容:

#if _DEBUG || _UNIT_TEST
  //Code here
#endif

If this is not possible, is there a workaround to achieve the same thing without having to duplicate the code using a #elseif?

如果这是不可能的,是否有一种方法可以在不使用#elseif复制代码的情况下实现相同的目标?

3 个解决方案

#1


65  

#if defined _DEBUG || defined _UNIT_TEST 
  //Code here 
#endif 

You could use AND and NOT operators as well. For instance:

你可以使用,也不能使用操作符。例如:

#if !defined _DEBUG && defined _UNIT_TEST 
  //Code here 
#endif 

#2


9  

#if takes any C++ expression of integral type(1) that the compiler manages to evaluate at compile time. So yes, you can use || and &&, as long as you use defined(SOMETHING) to test for definedness.

#如果使用任何c++的整数类型表达式(1),编译器会在编译时进行计算。是的,你可以使用||和&,只要你使用定义(某物)来测试定义性。

(1): well, it's a bit more restricted than that; for the nitty-gritty see the restrictions here (at "with these additional restrictions").

(1):嗯,这比那有一点限制;具体细节请参见这里的限制(参见“附加的限制”)。

#3


3  

#if defined(_DEBUG) || defined(_UNIT_TEST)
  //Code here
#endif

Also for the record, it's #elif, not #elseif.

另外,还有#elif,而不是#elseif。

#1


65  

#if defined _DEBUG || defined _UNIT_TEST 
  //Code here 
#endif 

You could use AND and NOT operators as well. For instance:

你可以使用,也不能使用操作符。例如:

#if !defined _DEBUG && defined _UNIT_TEST 
  //Code here 
#endif 

#2


9  

#if takes any C++ expression of integral type(1) that the compiler manages to evaluate at compile time. So yes, you can use || and &&, as long as you use defined(SOMETHING) to test for definedness.

#如果使用任何c++的整数类型表达式(1),编译器会在编译时进行计算。是的,你可以使用||和&,只要你使用定义(某物)来测试定义性。

(1): well, it's a bit more restricted than that; for the nitty-gritty see the restrictions here (at "with these additional restrictions").

(1):嗯,这比那有一点限制;具体细节请参见这里的限制(参见“附加的限制”)。

#3


3  

#if defined(_DEBUG) || defined(_UNIT_TEST)
  //Code here
#endif

Also for the record, it's #elif, not #elseif.

另外,还有#elif,而不是#elseif。