Easy preprocessor defines January 4th, 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                        +
#define OFF                       -
#define USING(x)                  ((1 x 1) == 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.

UPDATE: I have replaced the macros with the much better variation as suggested by Robert in the comments below.

Example usage:
#define BIG_ENDIAN                ON
// whatever

Written by Richard Mitton,

software engineer and travelling wizard.

Follow me on twitter: