C等同于C ++ decltype [英] C equivalent to C++ decltype

查看:92
本文介绍了C等同于C ++ decltype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C项目中,有一个由另一位同事创建的结构,其中包含一些函数指针:

In my C project, there is a struct, created by another colleague, containing some function pointers:

struct tools {  
    int (*tool_a) (int, int, int);
    ...
};

我无权更改此结构和相关文件.

I have no right to change this struct and relative files.

现在我正在使用struct进行编码.
我必须定义一个函数,其返回类型和参数列表必须与tools.tool_a相同.
这意味着我的功能必须如下:

Now I'm coding with the struct.
I have to define a function, whose return type and list of arguments must be the same with the tools.tool_a.
Meaning that my function must be as below:

int my_func(int, int, int);

问题在于结构变化很多,尤其是返回类型,例如今天intsize_t取代,所以我不得不改变很多代码.

The problem is that the struct changes a lot, especially the return types, for example int is replaced by size_t today, so I have to change my code a lot.

我知道C ++中的decltype可以帮助我,所以我只想知道C是否具有等效功能?

I know that decltype in C++ can help me so I just want to know if C has something equivalent?

我想我可以使用宏,但是我不知道怎么做,甚至不知道是否可能.

I'm thinking I may use macro but I don't know how, I even don't know if it's possible or not.

我正在使用C开发一些针对Linux内核的测试工具.
我公司的其他部门提供了许多版本的自定义内核.由于历史原因,其中一些使用int,另一些使用size_tssize_t,依此类推.

I'm developing some testing tools for linux-kernel with C.
There have been many versions of custom kernels coming from other groups in my company. For historical reasons, some of them used int, others used size_t or ssize_t and so on.

现在,当我编码时,我必须这样做:

Now when I code, I have to do like this:

// 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;
}

我必须保持评论和取消评论...

I have to keep commenting and uncommenting...

推荐答案

理智的解决方案是强制执行typedef.如果这不可能,并且该功能可以具有的替代类型的数量受到限制,那么似乎可以使用C11 _Generic来完成.

The sane solution is to enforce a typedef. If that isn't possible, and the number of alternative types the function could have are limited, as seems to be the case, you could cook up something with C11 _Generic.

创建具有不同名称的多个功能,而不是使用一个名为my_func的功能.前缀名称取决于返回类型.然后有一个宏,该宏根据传递的类型依次重定向到适当的函数.

Instead of having a single function called my_func, create multiple functions with different names. Prefix their names depending on the return type. Then have a macro which in turn re-directs to the appropriate function, based on the type passed.

示例:

#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);

}

在这里,我使用复合文字(struct tools){0}.tool_a创建与tool_a相同类型的虚拟对象,然后将其传递给选择适当功能的宏.如果不支持该类型,则将出现编译器错误,因为找不到匹配的_Generic关联.

Here I used a compound literal (struct tools){0}.tool_a to create a dummy object of the same type as tool_a, then passed that on to the macro which picks the appropriate function. If the type is not supported, there will be a compiler error since no matching _Generic association could be found.

这篇关于C等同于C ++ decltype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆