【发布时间】:2019-12-30 23:13:09
【问题描述】:
Cython 的 documentation 似乎对如何包装 user-defined conversion 保持沉默。
例如,当下面的 c++ 代码打印1(即true、here live)时:
#include <iostream>
struct X{
operator bool() const{ return true;}
};
int main() {
X x;
std::cout << x << "\n";
}
它在 Cython 中的“等价物”:
%%cython -+
cdef extern from *:
"""
struct X {
//implicit conversion
operator bool() const { return true; }
};
"""
cdef cppclass X:
operator bool() # ERROR HERE
def testit():
cdef X x;
print(x) # implicit cast, "should" print True
没有得到 cythonized 并显示以下错误消息(在标有 ERROR HERE 的行中):
'operator' 不是类型标识符
如何从 Cython 使用用户定义的转换,如果不是,有什么解决方法?
【问题讨论】: