4
votes

Let's 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 error message: "Macro argument mismatch", "Too few arguments provided to function-like invocation method", "Use of undeclared identifier 'macro_test_0' ".

Thing is, for macro_test_0, example gives:

int ( ) ;

which is ok, but macro_test_1 example gives (if I'm correct):

macro_test_0((,) ;)

which is obviously wrong. I'd like arg1 and arg2 of the macro to prevent expansion, in order to keep:

macro_test0(PARENTHESIS1, PARENTHESIS2 ;)

I guess it's related to macro expansion order, but is there a way or trick to achieve this ? I tried several things such as artificial (ie useless) concatenation of arguments to delay expansion during macro invocation but without success.

1
I don't think there's a simple solution. For advanced preprocessor stuff, you should generally look at Boost.Preprocessor first. Perhaps it will have a different solution for your larger problem.Angew is no longer proud of SO
Multi-level macro replacement, which depends on order of macro replacement, and also macro's which expand to parentheses (which are significant in macro syntax)? Problems are to be expected. Why do you need this? In C++, there's almost always a better solution.MSalters
Angew > I've been looking into boost preprocessor, but couldn't find a solution among the available macros. Open parentheses as arguments look totally cursed..brahmin

1 Answers

0
votes

i don't know which compiler or IDE u use.

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

int ( ) ; 
int ( ) ; 

for vs:

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

option will output code after preprocessing to a file

GCC have similar compile options.