How to make compilation unsuccessful if both preprocessor macros are defined or not

A friend asked me for help in C ++. I do not use only C ++ C # (WP, WPF, WinForms) and Java (Android).

Part of the task he has is when the STAR macro is defined, he should draw a christmass tree with * (stars), when the EQ macro is defined, he suggested that he should draw it with = (assign an operator). If both are undefined, compilation failed. Drawing a christmass tree is a simple task in any language, but I have problems with preprocessor macros.

#define STAR *
#define EQ =

#if !(defined(STAR) ^ defined(EQ)) 
   FAIL TO COMPILE?
#endif

How then to check in the code which macro is defined and assign its value char character;?

+4
source share
3
+15

:

#error "One and only one of STAR or EQ should be defined"
+5

I did this task for him. The preprocessor instructions are as follows:

 #define STAR "*"
// #define EQ "="
 #if !(defined(STAR) ^ defined(EQ))
      #error "STAR XOR EQ have to be defined"
 #elif (defined(STAR))
        #define CHARACTER STAR
 #elif (defined(EQ))
        #define CHARACTER EQ
 #endif
+2
source

Source: https://habr.com/ru/post/1532137/


All Articles