What's the simplest way to ensure that only one of two names is defined, for example:
#define USE_OPTION1
#define USE_OPTION2
#if not(USE_OPTION1 ^ USE_OPTION2)
#error "You must use at least one option, but not both"
#endif
I know there's no logical XOR in C or C++, so what's the best way of doing this? It doesn't have to be this, does it:
#define USE_OPTION1
#define USE_OPTION2
#ifdef USE_OPTION1
#ifdef USE_OPTION2
#error "You can't use both"
#endif
#endif
#ifdef USE_OPTION2
#ifdef USE_OPTION1
#error "You can't use both"
#endif
#endif
#ifndef USE_OPTION1
#ifndef USE_OPTION2
#error "You must use at least one"
#endif
#endif
you can solve this by checking if both are defined equally. so 0 0
and 1 1
will throw the error, while 0 1
and 1 0
are allowed.
#if defined(USE_OPTION1) == defined(USE_OPTION2)
#error "You must use at least one option, but not both"
#endif