简体   繁体   中英

Passing function parameters to a function through a macro

I want to create a macro in which one of the parameters is the parameters for a function used within the macro.

#define Macro(PERAMS, OTHER) \
    functionBeingUsed(PERAMS); \
    OTHER; 


Macro(1,2,3,4, int i = 0);

As you can see you can see the commas are being used by both the macro and the function. which results in broken code.

I was wondering if there was a way to achieve the result that I need so the code can be interpreted like the following.

Macro((1,2,3,4), int i = 0);

Please note I am not using C++11.

Whenever you need to do anything nontrivial with the preprocessor, you should immediately jump to the Boost.PP library. Here, the BOOST_PP_REMOVE_PARENS macro seems relevant:

#define Macro(PARAMS, OTHER) \
    functionBeingUsed(BOOST_PP_REMOVE_PARENS(PARAMS)); \
    OTHER; 

Macro((1,2,3,4), int i = 0);

Alternatively, you could just write functionBeingUsed PARAMS and let the passed parentheses be used.

It is not possible to do what you have described. However, you can do something similar with variadic macros . You would need to put the PARAMS part at the end and it would look like the following:

#define Macro(OTHER, ...) \
    functionBeingUsed(__VA_ARGS__); \
    OTHER;

Macro(int i = 0, 1, 2, 3, 4);

I believe the only answer is to use multiple macros

#define MacroBegin \
   \\PreFunction

#define MacroEnd \
   \\PostFunction

MacroBegin
functionBeingCalled(1,2,3,4);
MacroEnd

To differentiate which commas belong to the macro itself and which belong to the function being called, you have to wrap the function parameters in parenthesis, eg:

Macro((1,2,3,4), int i = 0);

You would then have to remove the parenthesis from the function call inside the macro, since they will be coming from the macro parameter instead, eg:

#define Macro(PERAMS, OTHER) \
    functionBeingUsed PERAMS; \
    OTHER; 

So, something like this:

Macro((1,2,3,4), int i = 0);

Will resolve to this:

functionBeingUsed (1,2,3,4);
int i = 0; 

Just remember the extra parenthesis even if the function being called doesn't take any input parameters:

Macro((), ...);

functionBeingUsed ();
...; 

To keep your syntax, you may do something like:

#define COUNT_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...)    N
#define COUNT(...)   COUNT_N(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
// Warning: COUNT() return 1 (as COUNT(A)) :-/

#define IDENTITY(N) N
#define APPLY(macro, ...) IDENTITY(macro(__VA_ARGS__))

#define F_1(a) functionBeingUsed(); a;
#define F_2(a, b) functionBeingUsed(a); b;
#define F_3(a, b, c) functionBeingUsed(a, b); c;
#define F_4(a, b, c, d) functionBeingUsed(a, b, c); d;
#define F_5(a, b, c, d, e) functionBeingUsed(a, b, c, d); e;
#define F_6(a, b, c, d, e, f) functionBeingUsed(a, b, c, d, e); f;
#define F_7(a, b, c, d, e, f, g) functionBeingUsed(a, b, c, d, e, f); g;
#define F_8(a, b, c, d, e, f, g, h) functionBeingUsed(a, b, c, d, e, f, g); h;

#define DISPATCH(N) F_ ## N

#define Macro(...) IDENTITY(APPLY(DISPATCH, COUNT(__VA_ARGS__)))(__VA_ARGS__)

Demo

but simpler to change your syntax to:

#define Macro(PARAMS, OTHER)  \
    functionBeingUsed PARAMS; \
    OTHER;

Macro((1,2,3,4), int i = 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM