Variadic Function Invoking a Variable Macro

I have a built-in variational function
inline int foo(...) I need foo() call a macro (let it be called MACRO ), which is also variational.
Basically, I need foo() to pass all its input parameters to MACRO . Overriding foo() as another macro would be a simple solution due to the __VA_ARGS__ option, but I also need foo() to return the value. Note. I am trying to relate the two parts of already written code, and I am not allowed to modify them. foo(...) used in the first part of the code, and MACRO is defined in the second part. The only thing I have to do is define foo() , which uses MACRO , and I cannot, because they are both variables.

+6
source share
2 answers

Make a foo macro containing a lambda that returns a value and then calls that lambda.

 #define foo(...) \ [&](auto&&...args){ \ /* do something with args, or __VA_ARGS__ */ \ MACRO(__VA_ARGS__); \ return 7; \ }(__VA_ARGS__) 

now int x = foo(a, b, c); both will call the lambda inside foo , and inside that lambda will call the macro on (a, b, c) and be able to return the value.

I'm sorry that you save your code further.

+6
source

What you ask for is impossible.

The arguments of the argument variable are determined at run time, but the macro expands at compile time.

+1
source

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


All Articles