简体   繁体   English

如何在一行中检查没有定义任何预处理器宏?

[英]How to check in a single line that none of the pre-processor macros are defined?

It's easy to check in a single line if at least one macro is defined: 如果至少定义了一个宏,则很容易检查一行:

#if defined(A) || defined(B) || defined(C)
    // do something
#endif

Also checking in a single line if at least one of the macros is not defined: 还要在一行中检查是否未定义至少一个宏:

#if !defined(A) || !defined(B) || !defined(C)
    // do something
#endif

Question: How to check in a single line that none of the macros are defined? 问题:如何在一行中检查所有宏均未定义?

I can do it with ifndefs in three lines as follows: 我可以在三行中使用ifndefs做到这一点,如下所示:

#ifndef A
#ifndef B
#ifndef C
    // do something
#endif
#endif
#endif

But how to join three ifndefs into a single line? 但是,如何将三个ifndefs合并为一行?

Emulating your nested #ifndef 's: 模拟嵌套的#ifndef

#if !defined(A) && !defined(B) && !defined(C)
  // do something
#endif

This checks that none are defined. 这检查没有定义。 You say you want "at least one isn't defined", but that's covered by your example with || 您说要“至少没有定义一个”,但是示例中用||覆盖了这一点。 s. s。

nested #ifndef can be joined on the same line with just && : 嵌套的#ifndef可以通过&&连接到同一行:

#if !defined(A) && !defined(B) && !defined(C)

#endif

Question: How to check in a single line that none of the macros are defined? 问题:如何在一行中检查所有宏均未定义?

  • To check if one macro is defined: #if defined A . 要检查是否定义了一个宏: #if defined A
  • To check if one macro is not defined: #if !defined A . 要检查是否未定义一个宏: #if !defined A "if not defined A". “如果未定义,则为A”。
  • To check if several macros are not defined: #if !defined A && !defined B && !defined C . 要检查是否未定义几个宏: #if !defined A && !defined B && !defined C
    "if not defined A and not defined B and not defined C" “如果未定义A,未定义B和未定义C”

Common sense usually gets you quite far in boolean algebra. 常识通常会使您在布尔代数方面走得更远。 To figure out the boolean equation for more complex cases, define truth tables. 要找出更复杂情况的布尔方程,请定义真值表。 Example: 例:

0 = false (not defined) 1 = true (defined) 0 =假(未定义)1 =真(已定义)

A B C Output
0 0 0 1
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 0

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

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