Easy preprocessor defines

Wednesday January 4, 2012

Here’s a nice little trick I’ve been using for a while. The trouble with the whole #if-vs-#ifdef debate is that both of them suck for different reasons.

So instead we can create a custom macro to handle some of this stuff:

#define ON              2-
#define OFF             1-
#define USING(x)        ( (x 0) == 2 )

The advantage here is that a macro is forced to either be defined to ON or OFF; if you don’t define it all you’ll get a compiler error.

Example usage:

#define BIG_ENDIAN          ON
#if USING( BIG_ENDIAN )
// whatever
#endif

— Kayamon
---

Wednesday January 4, 2012

(optional, only used to notify you of replies)
(optional)
 
---