I want to conditionally compile my code with macros.
The usual approach outlined in these questions: 1 2 3 4 5 is to #define A_MACRO "and give it a value"
, and then use #ifdef
or #if defined
to determine if it was defined.
Example:
//#define A 0
#define B 0
//#define C 0
#ifdef A
/*A stuff*/
#elif defined(B)
/*B stuff*/
#else
/*C stuff*/
#endif
I just had the idea to do something like:
#define A 0
#define B 1
#define C 2
#define LETTER B
#if LETTER == A
/*A stuff*/
#elif LETTER == B
/*B stuff*/
#else
/*C stuff*/
#endif
Are there any benefits or drawbacks of using one over the other? Personally I think the second is more readable and doesn't require to comment out macros if you are not using them. Also having to give macros a value that isn't used in any way is also counter-intuitive.