mort
mort

Reputation: 13608

Compiler should raise an error for certain combinations of #define

In a current project, I'm experimenting around a lot to see the performance influence of different solutions. Since I like to keep all the code, I have a lot of #ifdef directives, which make it easy for me to switch on and off some optimizations. However, some combinations of defines are not covered. I'd like to see a compiler error if this happens, i.e.:

#define A
#define B

#ifdef A
#ifdef B
//invalid combination of defines. Compiler should raise an error.
#endif
#endif

#ifdef A
//do something
#endif
#ifdef B
//do something else
#endif

Is that possible?

Upvotes: 1

Views: 247

Answers (4)

ouah
ouah

Reputation: 145919

#if defined(A) && defined(B)
#error invalid combination of defines
#endif

Upvotes: 2

user529758
user529758

Reputation:

Use the error Preprocessor directive:

#error "Invalid combination"

Upvotes: 2

Daniel Fischer
Daniel Fischer

Reputation: 183978

#ifdef A
#ifdef B
//invalid combination of defines. Compiler should raise an error.
#error Invalid combination
#endif
#endif

Upvotes: 2

kennytm
kennytm

Reputation: 523754

Yes. Just use the error directive (#error).

#ifdef A
#ifdef B
#error "invalid combination of defines."
#endif
#endif

Upvotes: 7

Related Questions