C 预处理器定义生成的函数名称
我遇到的情况是,我有相当多的生成函数,并且希望将它们指向我创建的一些通用函数(以允许我在生成的函数名称更改时重用基本代码)。
本质上,我有一个函数名称列表,如下所示:
void Callback_SignalName1(void);
void Callback_SignalName2(void);
...etc
生成这些函数名称后,我想定义一个宏以允许一般调用它们。 我的想法是这样的,但我没有任何运气实现它......因为 C 预处理器采用宏的名称而不是宏的定义:
#define SIGNAL1 SignalName1
#define SIGNAL2 SignalName2
#define FUNCTION_NAME(signal) (void Callback_ ## signal ## (void))
...
...
FUNCTION_NAME(SIGNAL1)
{
..
return;
}
问题是我收到
void Callback_SIGNAL1(void)
而不是
void Callback_SignalName1(void)
有一个好的方法解决这个问题吗?
I have a situation where I have quite a few generated functions, and would like to point them at some generic functions that I have created (to allow me to reuse the base code when the generated function names change).
Essentially, I have a list of function names as follows:
void Callback_SignalName1(void);
void Callback_SignalName2(void);
...etc
Once these are generated, I would like to define a macro to allow them to be called generically. My idea was this, but I haven't had any luck implementing it...as the C pre-processor takes the name of the macro instead of what the macro is defined as:
#define SIGNAL1 SignalName1
#define SIGNAL2 SignalName2
#define FUNCTION_NAME(signal) (void Callback_ ## signal ## (void))
...
...
FUNCTION_NAME(SIGNAL1)
{
..
return;
}
The issue is that I receive
void Callback_SIGNAL1(void)
instead of
void Callback_SignalName1(void)
Is there a good way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要提供额外级别的“类函数宏”以确保正确扩展:
例如
输出:
You need to provide an extra level of "function-like macro" to ensure the proper expansion:
e.g.
output: