我如何检查,如果一个变量是某种类型的C(比较两种类型)? [英] How do I check if a variable is of a certain type (compare two types) in C?
问题描述
在C(不是C ++ / C#)我该如何检查,如果一个变量是某种类型的。例如这样的事情:
In C (not C++/C#) how do I check if a variable is of a certain type. For example something like this:
double doubleVar;
if( typeof(doubleVar) == double ) {
printf("doubleVar is of type double!");
}
或者更一般的:我怎样比较两个类型,以便比较(double1,double2)
将evalute真实和比较(INT,双)
将评估假的。此外,我想比较不同的组成结构为好。感谢您的帮助。
Or more general: How do I compare two types so that compare(double1,double2)
will evalute true and compare(int,double)
will evaluate false. Also I'd like to compare structs of different composition as well. Thanks for any help.
修改:
基本上我有对类型结构a和结构B的变量操作的功能。我想做一件事的结构体一个变量和其他与结构B的变量。由于C不支持重载和无效
指针损失的类型信息,我需要检查的类型。顺便说一句。这将是在有的typeof
运营商SENCE,如果你不能比较类型?
Edit:
Basically I have a function that operates on variables of type "struct a" and "struct b". I want to do one thing with the "struct a" variables and the other with the "struct b" variables. Since C doesn't support overloading and the void
pointer losses its type information I need to check for type. Btw. what would be the sence in having a typeof
operator, if you can't compare types?
EDIT2 :
sizeof运算法似乎是对我来说是切实可行的解决方法解决方案。谢谢你的帮助。我还觉得有点奇怪,因为类型知道编译时,但如果我想象中的机器,我可以看到,为什么信息不会存储在类型方面,而是在字节大小方面的进程。尺寸是除了地址真正相关的唯一的事情。
Edit2: The sizeof-method seems to be a practical workaround solution for me. Thanks for your help. I still find it a bit strange since the types are know at compiletime but if I imagine the processes in the machine I can see, why the information is not stored in terms of types but rather in terms of byte size. Size is the only thing really relevant besides addresses.
推荐答案
获取一个变量的类型是,截至目前,有可能在C11与 _Generic
通用选择。它的工作原理在编译时。
Getting the type of a variable is, as of now, possible in C11 with the _Generic
generic selection. It works at compile-time.
该构造结是有点像开关
。下面是一个示例(从这个答案):
The syntaxis is a bit like switch
. Here's a sample (from this answer):
#define typename(x) _Generic((x), \
_Bool: "_Bool", unsigned char: "unsigned char", \
char: "char", signed char: "signed char", \
short int: "short int", unsigned short int: "unsigned short int", \
int: "int", unsigned int: "unsigned int", \
long int: "long int", unsigned long int: "unsigned long int", \
long long int: "long long int", unsigned long long int: "unsigned long long int", \
float: "float", double: "double", \
long double: "long double", char *: "pointer to char", \
void *: "pointer to void", int *: "pointer to int", \
default: "other")
要真正使用它的编译时手动类型检查,你可以定义一个枚举
与所有你所期望的类型,像这样:
To actually use it for compile-time manual type checking, you can define an enum
with all of the types you expect, something like this:
enum t_typename {
TYPENAME_BOOL,
TYPENAME_UNSIGNED_CHAR,
TYPENAME_CHAR,
TYPENAME_SIGNED_CHAR,
TYPENAME_SHORT_INT,
TYPENAME_UNSIGNED_CHORT_INT,
TYPENAME_INT,
/* ... */
TYPENAME_POINTER_TO_INT,
TYPENAME_OTHER
};
然后用 _Generic
来匹配类型本枚举
:
#define typename(x) _Generic((x), \
_Bool: TYPENAME_BOOL, unsigned char: TYPENAME_UNSIGNED_CHAR, \
char: TYPENAME_CHAR, signed char: TYPENAME_SIGNED_CHAR, \
short int: TYPENAME_SHORT_INT, unsigned short int: TYPENAME_UNSIGNED_SHORT_INT, \
int: TYPENAME_INT, \
/* ... */ \
int *: TYPENAME_POINTER_TO_INT, \
default: TYPENAME_OTHER)
这篇关于我如何检查,如果一个变量是某种类型的C(比较两种类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!