I have the following code
#define myfunc(a,b) myfunc(do_a(a), do_b(b))
void myfunc(int a, int b)
{
do_blah(a,b);
}
int main()
{
int x = 6, y = 7;
myfunc(x,y);
return 0;
}
I want the pre-processor to expand function myfunc only at calling. Required code after pre-processing looks like this:
void myfunc(int a, int b)
{
do_blah(a,b);
}
int main()
{
int x = 6, y = 7;
myfunc(do_a(x),do_b(y));
return 0;
}
The problem is that function definition is expanded also like this
void myfunc(do_a(int a), do_b(int b))
{
do_blah(a,b);
}
Is there any way to make macro expands only if we are expanding a function call? I tried many solutions, and it seems impossible but I hope that some one saw situation like this..
NOTE: please don't tell me to rename the macro or function names :D
Update1: Thanks for you help. But I can only change the definition of the macro, I can't change its position and I can't change function implementation.
It is not possible.
A macro is defined with the pre-processor directive. Macros are pre-processed which means that all the macros would be processed before your program compiles. However, functions are not preprocessed but compiled.
Just type the word Call then space, then type the name of the macro to be called (run). The example below shows how to call Macro2 from Macro1. It's important to note that the two macros DO NOT run at the same time. Once the Call line is hit, Macro2 will be run completely to the end.
Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z' , 'A-Z' , '0-9' , and '_' , and the first character should not be a digit.
Use ()
to stop the preprocessor from expanding the function definition:
#include <stdio.h>
#define myfunc(a, b) myfunc(do_a(a), do_b(b))
/* if you have a preprocessor that may be non-standard
* and enter a loop for the previous definition, define
* myfunc with an extra set of parenthesis:
#define myfunc(a, b) (myfunc)(do_a(a), do_b(b))
******** */
int (myfunc)(int a, int b) /* myfunc does not get expanded here */
{
printf("a=%d; b=%d\n", a, b);
return 0;
}
int do_a(int a)
{
return a * 2;
}
int do_b(int b)
{
return b - 5;
}
int main(void)
{
myfunc(4, 0);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With