宏和函数同名
我有以下代码,
#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;
}
我希望预处理器仅在调用时扩展函数 myfunc。预处理后所需的代码如下所示:
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;
}
问题是函数定义也像这样展开
void myfunc(do_a(int a), do_b(int b))
{
do_blah(a,b);
}
有没有办法使宏仅在我们展开函数调用时展开? 我尝试了很多解决方案,这似乎不可能,但我希望有人看到这样的情况..
注意:请不要告诉我重命名宏或函数名称:D
Update1: 谢谢你的帮助。但我只能改变宏的定义,不能改变它的位置,也不能改变函数的实现。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在函数定义之后定义宏。
Define the macro after the defintion of the function.
定义函数后定义宏。
或者,使用如下模式:
Define the macro after you define the function.
Alternatively, use a pattern like this:
使用
()
阻止预处理器扩展函数定义:Use
()
to stop the preprocessor from expanding the function definition:我看到三种可能的解决方案:
在函数定义之后定义宏。
在函数定义之前定义
do_a()
和do_b()
,以便它们返回它们的参数,并在函数定义之后根据您的意愿重新定义它们执行 <函数内的 code>do_a() 和
do_b()
:我非常倾向于后者。
I see three possible solutions:
define your macro after function definition.
define, before the function definition,
do_a()
anddo_b()
such that they return their argument, and redefine them at your will after function definitionperform
do_a()
anddo_b()
inside the function:I have a strong preference for the latter.