[英]Cython: Nesting a union within a struct
在Cython glue声明中,我如何表示包含匿名联合的C struct
类型? 例如,如果我有一个包含的C头文件mystruct.h
struct mystruct
{
union {
double da;
uint64_t ia;
};
};
然后,在相应的.pyd
文件中
cdef extern from "mystruct.h":
struct mystruct:
# what goes here???
我试过这个:
cdef extern from "mystruct.h":
struct mystruct:
union {double da; uint64_t ia;};
但这只在union
线上给了我“C变量声明中的语法错误”。
对于那些通过谷歌来到这里的人,我找到了解决方案。 如果你有一个结构:
typedef struct {
union {
int a;
struct {
int b;
int c;
};
}
} outer;
您可以在Cython声明中将其全部展平,如下所示:
ctypedef struct outer:
int a
int b
int c
Cython没有生成任何代码来对结构的内存布局做出任何假设; 你只是通过告诉它生成什么语法来调用它来告诉它你所调用的事实结构。 所以,如果你的结构有大小的成员int
可作为访问((outer) x).a
,那么你就可以抛出a
在结构定义,它会工作。 它是在文本替换而不是内存布局上运行的,所以它不关心这些东西是匿名联合或结构还是你有什么。
您无法根据我的知识嵌套声明,并且Cython不支持匿名联合AFAIK。
请尝试以下方法:
cdef union mystruct_union:
double lower_d
uint64_t lower
cdef struct mystruct:
mystruct_union un
现在以un.lower_d
和un.lower
访问union成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.