talamaki
talamaki

Reputation: 5472

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:

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

But how to join three ifndefs into a single line?

Upvotes: 0

Views: 249

Answers (3)

Lundin
Lundin

Reputation: 214060

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.
  • To check if one macro is not defined: #if !defined A. "if not defined A".
  • To check if several macros are not defined: #if !defined A && !defined B && !defined C.
    "if not defined A and not defined B and not defined 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)

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

Upvotes: 2

Jean-François Fabre
Jean-François Fabre

Reputation: 140196

nested #ifndef can be joined on the same line with just &&:

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

#endif

Upvotes: 3

orip
orip

Reputation: 75457

Emulating your nested #ifndef's:

#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.

Upvotes: 4

Related Questions