Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use "else if" with the preprocessor #ifdef?

In my project, the program can do one thing of two, but never both, so I decided that the best I can do for one class is to define it depending of a #define preprocessor variable. The following code can show you my idea, but you can guess that it does not work:

#ifdef CALC_MODE
typedef MyCalcClass ChosenClass;
#elifdef USER_MODE
typedef MyUserClass ChosenClass;
#else
static_assert(false, "Define CALC_MODE or USER_MODE");
#endif

So I can do

#define CALC_MODE

right before this.

I can resign the use of static_assert if needed. How can I do this?

like image 904
Jonnas Kraft Avatar asked Sep 06 '25 03:09

Jonnas Kraft


1 Answers

Here's a suggestion, based largely on comments posted to your question:

#if defined(CALC_MODE)
    typedef MyCalcClass ChosenClass;
#elif defined(USER_MODE)
    typedef MyUserClass ChosenClass;
#else
    #error "Define CALC_MODE or USER_MODE"
#endif
like image 54
Adrian Mole Avatar answered Sep 07 '25 23:09

Adrian Mole