【问题标题】:The type pointed by pointer at compile time like typeof编译时指针指向的类型,如 typeof
【发布时间】:2018-03-30 21:52:31
【问题描述】:

有没有办法获取指针所指向的数据类型?我对与 typeof 类似的东西感兴趣。我希望我能用 sn-p 代码更好地解释它:

struct_t *pointer1;
struct_t **pointer2;
typeof_ptr(pointer) a;  // here a should be of type struct_t
typeof_ptr(pointer) b;  // here b should also be of type struct_t

【问题讨论】:

    标签: c gcc clang


    【解决方案1】:

    您可以使用typeof(*pointer1),但typeof(*pointer2) 将是struct_t* 而不是struct_t;你需要做typeof(**pointer2)

    请记住,typeof() 是 GCC 扩展。

    【讨论】:

    • 我事先不知道它将是什么样的指针。我需要它来做一个宏。我知道 typeof 是一个扩展。
    • 老实说,如果没有相应的方法来实际检索值,我真的看不到它的用途,但如果这真的是你需要的,那么答案是否定的。这闻起来像 XY problem……也许你应该退后一步,想想你为什么认为你需要这个。
    • 这更像是在 c 中做某种类的练习,想法是将指向调用函数的对象的指针存储在全局变量中,这样做我在想象一些事情像这样#define O(x) (((typeof_ptr(&x) *)class_handle(&x))->vt) 和函数将被称为 O(object)->member() 或 O(object_ptr)->member()。每个类都有一个名为 vt 的成员,它实际上存储了成员函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2010-10-22
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多