Yve*_*ves 11 c c++ macros decltype
在我的C项目中,有一个由另一位同事创建的结构,包含一些函数指针:
struct tools {
int (*tool_a) (int, int, int);
...
};
Run Code Online (Sandbox Code Playgroud)
我无权更改此结构和相关文件.
现在我用结构编码.
我必须定义一个函数,其返回类型和参数列表必须与tools.tool_a.
这意味着我的功能必须如下:
int my_func(int, int, int);
Run Code Online (Sandbox Code Playgroud)
问题是结构变化很大,特别是返回类型,例如今天int被替换size_t,所以我必须经常更改我的代码.
我知道decltype在C++中可以帮助我所以我只想知道C是否有相同的东西?
我想我可能会使用宏但我不知道如何,我甚至不知道它是否可能.
我正在用C开发一些用于Linux内核的测试工具
.我公司的其他组有很多版本的自定义内核.由于历史原因,其中一些使用int,其他使用size_t或ssize_t等等.
现在当我编码时,我必须这样做:
// int my_func(int a, int b, int c)
size_t my_func(int a, int b, int c)
// ssize_t my_func(int a, int b, int c)
{}
struct tools my_tool = {
.tool_a = my_func;
}
Run Code Online (Sandbox Code Playgroud)
我必须继续评论和取消评论......
理智的解决方案是强制执行typedef.如果这是不可能的,并且函数可能具有的替代类型的数量是有限的,就像在这种情况下,你可以用C11烹饪_Generic.
而不是调用单个函数my_func,创建具有不同名称的多个函数.根据返回类型为其名称添加前缀.然后有一个宏,它反过来根据传递的类型重定向到适当的函数.
例:
#include <stdio.h>
/*** the struct that cannot be changed ***/
struct tools {
int (*tool_a) (int, int, int);
};
/*** any number of functions with different types ***/
int int_my_func(int a, int b, int c)
{
puts(__func__);
}
size_t size_t_my_func(int a, int b, int c)
{
puts(__func__);
}
/*** macro to select the appropriate function based on type ***/
#define my_func_typeof(type) \
_Generic( (type), \
int(*)(int,int,int) : int_my_func, \
size_t(*)(int,int,int) : size_t_my_func)
/*** caller code ***/
int main (void)
{
struct tools my_tool = {
.tool_a = my_func_typeof( (struct tools){0}.tool_a )
};
my_tool.tool_a(1,2,3);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用复合文字(struct tools){0}.tool_a来创建一个与之相同类型的虚拟对象tool_a,然后将其传递给选择适当函数的宏.如果不支持该类型,则会出现编译器错误,因为找不到匹配的_Generic关联.
好吧,这不是,decltype但如果您可以说服您的同事使用类型别名,您可以进行静态类型检查.
如果可以说服你的同事这样做:
typedef int tool_a_prototype(int, int, int);
struct tools {
tool_a_prototype *tool_a;
};
Run Code Online (Sandbox Code Playgroud)
然后你可以声明你的函数:
tool_a_prototype my_tool_a;
int my_tool_a(int a, int b, int c) {
//Whatever
}
Run Code Online (Sandbox Code Playgroud)
您的友好编译器会告诉您原型是否存在不匹配.
| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |