Reputation: 5472
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
Reputation: 214060
Question: How to check in a single line that none of the macros are defined?
#if defined A
.#if !defined A
. "if not defined A".#if !defined A && !defined B && !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
Reputation: 140196
nested #ifndef
can be joined on the same line with just &&
:
#if !defined(A) && !defined(B) && !defined(C)
#endif
Upvotes: 3
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