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,438
  • 68
  • 55
  • 126
mort
  • 12,118
  • 14
  • 47
  • 95

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
  • 491,404
  • 99
  • 1,053
  • 989
2
#ifdef A
#ifdef B
//invalid combination of defines. Compiler should raise an error.
#error Invalid combination
#endif
#endif
Daniel Fischer
  • 178,696
  • 16
  • 303
  • 427
2

Use the error Preprocessor directive:

#error "Invalid combination"
2
#if defined(A) && defined(B)
#error invalid combination of defines
#endif
ouah
  • 138,975
  • 15
  • 262
  • 325