获取一个变量的类型用C code [英] Getting the type of a variable in C code

查看:116
本文介绍了获取一个变量的类型用C code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,我可以在C自动发现一个变量的类型任何方式,无论是通过程序本身,或在某种机制 - 更容易 - 通过使用编译器的一个通行证pre-编译脚本到那里已经被解析的变量和分配他们自己的类型呢?我在寻找这个一般性建议。下面是更多的背景了解我需要什么,以及为什么。

Is there any way that I can discover the type of a variable automatically in C, either through some mechanism within the program itself, or--more likely--through a pre-compilation script that uses the compiler's passes up to the point where it has parsed the variables and assigned them their types? I'm looking for general suggestions about this. Below is more background about what I need and why.

我想改变的OpenMP reduction子句的语义。在这一点上,似乎最容易简单地更换源$ C ​​$ c。与一个函数的调用该条款(通过脚本),然后我可以定义函数来实现减少语义我想要的。例如,我的脚本将其转换

I would like to change the semantics of the OpenMP reduction clause. At this point, it seems easiest simply to replace the clause in the source code (through a script) with a call to a function, and then I can define the function to implement the reduction semantics I want. For instance, my script would convert this

#pragma omp parallel for reduction(+:x)

这个:

my_reduction(PLUS, &x, sizeof(x));
#pragma omp parallel for

在哪里,早些时候,我有(说)

where, earlier, I have (say)

enum reduction_op {PLUS, MINUS, TIMES, AND,
  OR, BIT_AND, BIT_OR, BIT_XOR, /* ... */};

my_reduction 有签名

void my_reduction(enum reduction_op op, void * var, size_t size);

在其他方面, my_reduction 将不得不加法运算应用到还原变量作为程序员原本准备。但我的函数不能知道如何正确地做到这一点。特别是,虽然它知道那种操作( PLUS ),原变量的位置( VAR ),和变量的类型的大小,它不知道变量的类型本身。尤其是,它不知道无功是否有一个整体或浮点型。从一个低级别的POV,对​​于这两个类的类型的加法运算是完全不同的。

Among other things, my_reduction would have to apply the addition operation to the reduction variable as the programmer had originally intended. But my function cannot know how to do this correctly. In particular, although it knows the kind of operation (PLUS), the location of the original variable (var), and the size of the variable's type, it does not know the variable's type itself. In particular, it does not know whether var has an integral or floating-point type. From a low-level POV, the addition operation for those two classes of types is completely different.

如果只非标准运营商的typeof ,其中GCC支持,将工作sizeof的工作方式 - 返回某种类型的变量 - 我可以很容易地解决这个问题。但是typeof运算是不是真的sizeof相似:它只能被使用,显然,在L值声明

If only the nonstandard operator typeof, which GCC supports, would work the way sizeof works--returning some sort of type variable--I could solve this problem easily. But typeof is not really like sizeof: it can only be used, apparently, in l-value declarations.

现在,编译器显然不知道x的类型它完成生成可执行code之前。这使我怀疑我是否能以某种方式利用GCC的解析器,只是为了让 X的类型,并将其传递给我的脚本,然后再次运行GCC,一路,编译我改变源$ C ​​$ C。那么这将是很简单的声明

Now, the compiler obviously does know the type of x before it finishes generating the executable code. This leads me to wonder whether I can somehow leverage GCC's parser, just to get x's type and pass it to my script, and then run GCC again, all the way, to compile my altered source code. It would then be simple enough to declare

enum var_type { INT8, UINT8, INT16, UINT16, /* ,..., */ FLOAT, DOUBLE};
void my_reduction(enum reduction_op op, void * var, enum var_type vtype);

my_reduction 可以适当提领和应用运营商之前投。

And my_reduction can cast appropriately before dereferencing and applying the operator.

正如你所看到的,我想为什么不直接使用C ++重载创造一种在C分派机制?因为我的项目限制我用C写的旧式源$ C ​​$ C工作,我可以用一个脚本自动改变code,但我不能把它改写成不同的语言。

As you can see, I am trying to create a kind of "dispatching" mechanism in C. Why not just use C++ overloading? Because my project constrains me to work with legacy source code written in C. I can alter the code automatically with a script, but I cannot rewrite it into a different language.

谢谢!

推荐答案

C11 _Generic 不是一个直接的解决方案,但是它允许你达到预期的结果,如果你有耐心,以code所有类型为:

C11 _Generic is not a direct solution, but it does allow you to achieve the desired result if you are patient to code all types as in:

#define typename(x) _Generic((x), \
    int:     "int", \
    float:   "float", \
    default: "other")

int i;
float f;
void* v;
assert(strcmp(typename(i), "int")   == 0);
assert(strcmp(typename(f), "float") == 0);
assert(strcmp(typename(i), "other") == 0);

与吨的各类一个很好的出发点可以在这个答案找到

A good starting point with tons of types can be found in this answer.

GCC仅在4.9增加了支持。

GCC only added support in 4.9.

这篇关于获取一个变量的类型用C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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