C / C ++ prevents macro scope inside a macro

Consider:

#define PARENTHESIS1 (
#define PARENTHESIS2 )
#define macro_test_0(arg1, arg2) int arg1 arg2
#define macro_test_1(arg1, arg2) macro_test_0(arg1, arg2)

macro_test_0(PARENTHESIS1, PARENTHESIS2 ;) //->works fine
macro_test_1(PARENTHESIS1, PARENTHESIS2 ;) //doesn't work

      

For macro_test_1, I have an error message: "Macro arguments mismatch", "Too few arguments provided to functionally like calling method", "Using undeclared identifier" macro_test_0 ".

Thing is, for macro_test_0, the example gives:

int ( ) ;

      

which is good, but the example macro_test_1 gives (if I'm right):

macro_test_0((,) ;)

      

which is obviously not true. I would like the arg1 and arg2 macros to prevent expansion, in order to keep:

macro_test0(PARENTHESIS1, PARENTHESIS2 ;)

      

I am guessing it has to do with ordering macro distribution, but is there a way or trick to achieve this? I've tried several things like artificial (i.e. useless) argument concatenation to delay expansion during macro invocation, but no success.

+3


source to share


1 answer


I don't know which compiler or IDE you are using.

but for visual studio 2012: both lines generate the same code after preprocessing

int ( ) ; 
int ( ) ; 

      

for vs:



'src file' property -> C / C ++ / Preprocessor -> preprocess to file

option outputs code after preprocessing to file

GCC have similar compilation options.

0


source







All Articles