#ifdef inside #define

I am trying to write something like this:

#define COV_ON(x) \ #ifdef COVERAGE_TOOL \ _Pragma (COVERAGE #x) #endif 

Is there a way to define COV_ON like this? I know what I did above incorrectly, because I cannot #ifdef inside #define. ( # not a valid character in #define ). So is there any solution?

+45
c c-preprocessor
Apr 7 2018-11-11T00:
source share
6 answers

Impossible. Do it the other way around:

 #ifdef COVERAGE_TOOL #define COV_ON(x) _Pragma (COVERAGE #x) #else #define COV_ON(x) #endif 
+57
Apr 07 2018-11-11T00:
source share

Just rotate it:

 #ifdef COVERAGE_TOOL #define COV_ON(x) _Pragma (COVERAGE #x) #else #define COV_ON(x) /* foo */ #endif 
+19
Apr 07 '11 at 19:25
source share
 #ifdef COVERAGE_TOOL #define COV_ON(x) _Pragma (COVERAGE #x) #else #define COV_ON(x) #endif 
+5
Apr 07 '11 at 19:25
source share

You can not. But you can change #ifdef and #define :

 #ifdef COVERAGE_TOOL # define COV_ON(x) _Pragma (COVERAGE #x) #else # define COV_ON(x) #endif 
+4
Apr 7 '11 at 19:25
source share

As you mentioned, it is not possible to set #ifdef to #define. Instead, you should reorder:

 #ifdef COVERAGE_TOOL \ #define COV_ON(x) \ etc. #endif 
+2
Apr 7 2018-11-11T00:
source share

This is an old question, but he needed a relevant answer.

Instead of using the built-in ifdef inside the macro, you can selectively define the __VA_ARGS__ macro to do the same

 #ifdef COVERAGE_TOOL #define IF_COVERAGE_TOOL(...) __VA_ARGS__ #else #define IF_COVERAGE_TOOL(...) #endif #define COV_ON(x) IF_COVERAGE_TOOL( _Pragma (COVERAGE #x) ) 

This has similar functionality for ifdef, except that you get parentheses to determine the beginning and end (most IDEs have no problem folding the code). Although you can use #define and #ifdef in context, #include not allowed. To get built-in features similar to #else , you can define the corresponding macro as follows:

 //#define FOO #ifdef FOO #define IF_FOO(...) __VA_ARGS__ #define NO_FOO(...) #else #define IF_FOO(...) #define NO_FOO(...) __VA_ARGS__ #endif IF_FOO( #define BAR 5 int foo = BAR; ) NO_FOO( #define foo 5 ) 

Only one of NO_FOO()/IF_FOO will generate code.

Well, this is a convenient hack, but can we make it more useful than #ifdefs ... Perhaps there is logical logic and configuration? Allows you to configure some truth tables (and auxiliary helper macros).

 #define PASTE_(x,y) x##y #define PASTE(x,y) PASTE_(x,y) #define PASTE3_(x,y,z) x##y##z #define PASTE3(x,y,z) PASTE3_(x,y,z) #define Y(...) __VA_ARGS__ #define N(...) #define IF(x) x //alternate method similar to IFNOT() #define NOT_N Y #define NOT_Y N #define IF_NOT(x) PASTE(NOT_,x) #define NOT(x) PASTE(NOT_,x) #define N_OR_N N #define N_OR_Y Y #define Y_OR_N Y #define Y_OR_Y Y #define OR(x,y) PASTE3(x,_OR_,y) #define N_AND_N N #define N_AND_Y N #define Y_AND_N N #define Y_AND_Y Y #define AND(x,y) PASTE3(x,_AND_,y) #define N_XOR_N N #define N_XOR_Y Y #define Y_XOR_N Y #define Y_XOR_Y N #define XOR(x,y) PASTE3(x,_XOR_,y) #define N_NOR_N Y #define N_NOR_Y N #define Y_NOR_N N #define Y_NOR_Y N #define NOR(x,y) PASTE3(x,_NOR_,y) #define N_NAND_N Y #define N_NAND_Y Y #define Y_NAND_N Y #define Y_NAND_Y N #define NAND(x,y) PASTE3(x,_NAND_,y) #define N_XNOR_N Y #define N_XNOR_Y N #define Y_XNOR_N N #define Y_XNOR_Y Y #define XNOR(x,y) PASTE3(x,_XNOR_,y) #define IF2(x,y,z) PASTE3(x,y,z) 

config.h

 #define FOO Y #define BAR N #define BAZ Y 

code.c

 AND(FOO,BAR)(/*do stuff if both FOO and BAR are enabled*/) IF2(FOO,_AND_,BAR)( /*do stuff if both FOO and BAR are enabled*/ ) OR(BAZ,AND(FOO,BAR))( /*do stuff if both FOO and BAR are enabled or BAZ is enabled*/ ) 
+1
Apr 16 '17 at 21:33
source share



All Articles