3

Essentially I got four cases in existing code:

  1. macro ABC is unset
  2. macro ABC is set, but empty: #define ABC or -DABC
  3. macro ABC is set, and evaluates to true: #define ABC 1 or -DABC=1
  4. macro ABC is set, and evaluates to false: #define ABC 0 or -DABC=0

I want the 1st and 4th, and 2nd and 3rd case to be the same:

#if defined(ABC) && IS_EMPTY(ABC)
#   undef ABC
#   define ABC 1
#endif

#if !defined(ABC) || !(ABC)
#   undef ABC
#   define ABC 0
#endif

How do I do IS_EMPTY(X)?

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • Same question here : http://stackoverflow.com/questions/3781520/how-to-test-if-preprocessor-symbol-is-defined-but-has-no-value – Co_42 Oct 10 '13 at 14:44

1 Answers1

3

If you know that only the values 0, 1 or empty can occur you could use something like

#define some_impossible_macro 1
#define some_impossible_macro0 0
#define some_impossible_macro1 0

#define IS_EMPTY(X) some_impossible_macro ## X

Otherwise, for the general case this is a bit more complicated but doable. You could use P99_IS_EMPTY from P99.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177