中检查变量是否属于某种类型

中检查变量是否属于某种类型

本文介绍了如何在 C 中检查变量是否属于某种类型(比较两种类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C(不是 C++/C#)中,如何检查变量是否属于某种类型?

In C (not C++/C#) how do I check if a variable is of a certain type?

例如,像这样:

double doubleVar;
if( typeof(doubleVar) == double ) {
    printf("doubleVar is of type double!");
}

或者更一般的:我如何比较两种类型,以便 compare(double1,double2) 将评估为真,而 compare(int,double) 将评估为错误的.我也想比较不同组成的结构.

Or more general: How do I compare two types so that compare(double1,double2) will evaluate to true, and compare(int,double) will evaluate to false. Also I'd like to compare structs of different composition as well.

基本上,我有一个对struct a"和struct b"类型的变量进行操作的函数.我想用struct a"变量做一件事,用struct b"变量做另一件事.由于 C 不支持重载并且 void 指针丢失了它的类型信息,我需要检查类型.顺便说一句,如果您无法比较类型,那么拥有 typeof 运算符有什么意义?

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 sense in having a typeof operator, if you can't compare types?

sizeof 方法对我来说似乎是一个实用的解决方案.谢谢你的帮助.我还是觉得有点奇怪,因为类型在编译时就知道了,但是如果我想象一下我可以看到的机器中的进程,为什么信息不是按照类型存储的,而是按照字节大小存储的.除了地址之外,大小是唯一真正相关的东西.

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 known at compile time, 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.

语法有点像switch.这是一个示例(来自这个答案):

The syntax is a bit like that for 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")

要实际使用它进行编译时手动类型检查,您可以使用您期望的所有类型定义一个 enum,如下所示:

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将类型匹配到这个enum:

And then use _Generic to match types to this enum:

    #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 中检查变量是否属于某种类型(比较两种类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 16:31