1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mort
  • 12,988
  • 14
  • 52
  • 97

4 Answers4

8

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

#ifdef A
#ifdef B
#error "invalid combination of defines."
#endif
#endif
Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2
#ifdef A
#ifdef B
//invalid combination of defines. Compiler should raise an error.
#error Invalid combination
#endif
#endif
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
2

Use the error Preprocessor directive:

#error "Invalid combination"
2
#if defined(A) && defined(B)
#error invalid combination of defines
#endif
ouah
  • 142,963
  • 15
  • 272
  • 331