hfossli
hfossli

Reputation: 22972

How to test if defined macro is empty at compile time (Cbjective-C / c)?

Is it possible to do check if I've got an empty define? IS_EMPTY_OR_UNDEFINED is a fictive macro I just came up with.

#define constantA 0
#define constantB 1
#define constantC null
#define constantF ""
#define constantH 

#if IS_EMPTY_OR_UNDEFINED(constantA)
# error constantA is defined 0 and the above if should not be true - this line should not run
#endif

#if IS_EMPTY_OR_UNDEFINED(constantB)
# error constantB is defined 1 and the above if should not be true - this line should not run
#endif

#if IS_EMPTY_OR_UNDEFINED(constantC)
# error constantC is defined null and the above if should not be true - this line should not run
#endif

#if IS_EMPTY_OR_UNDEFINED(constantF)
# error constantF is defined "" and the above if should not be true - this line should not run
#endif

#if ! IS_EMPTY_OR_UNDEFINED(constantH)
# error constantH is defined empty and the above if should not be true - this line should not run
#endif

#if defined(undefinedConstant) && ! IS_EMPTY_OR_UNDEFINED(undefinedConstant)
# error undefinedConstant is not defined and the above if should not be true - this line should not run
#endif

Upvotes: 2

Views: 6355

Answers (2)

Jens Gustedt
Jens Gustedt

Reputation: 78943

Checking if an expression is empty can be done (modulo some really exotic border case) but the technique is somewhat complicated. P99 has a macro that does this and which you could be using as

#if !defined(constantA) || P99_IS_EMPTY(constantA)
...
#endif

Combining this in one single macro is not allowed by the C standard. AS of C11 6.10.1 p4, the token defined is not allowed inside macros or other expressions.

Upvotes: 3

lethal-guitar
lethal-guitar

Reputation: 4519

You can use #ifdef MACRO or #if defined(MACRO) to test if macros are defined at all. You can also compare them, although only against integers:

#if MACRO > 5

etc. Maybe this helps you?

Edit: Although I don't know how you would check if a definition evaluates to "empty", or if this is possible.

Upvotes: -1

Related Questions