Cython:如何使用用户定义的C ++类转换? [英] Cython: How user-defined conversion of C++-classes can be used?

查看:87
本文介绍了Cython:如何使用用户定义的C ++类转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cython的文档似乎是对如何包装用户定义的转换保持沉默。

Cython's documentation seems to be silent about how user-defined conversion can be wrapped.

例如,以下c ++代码显示 1 (即 true 在此处直播):

For example, while the following c++-code prints 1 (i.e. true, here live):

#include <iostream>

struct X{
    operator bool() const{ return true;}    
};

int main() {
    X x;
    std::cout << x << "\n";
}

其在Cython中的等效值:

its "equivalent" in 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

没有得到用以下错误消息进行了cython化(在标有 ERROR c的行中):

doesn't get cythonized with the following error message (in the line marked with ERROR HERE):


运算符不是类型标识符

'operator' is not a type identifier

如何从Cython使用用户定义的转换,如果没有,解决方法是什么? / p>

How user-defined conversion can be used from Cython and if not what are workarounds?

推荐答案

仅查看 bool 情况:


  1. 我不认为 print(x)应该将其转换为bool。 print(x)寻找到Python对象的转换(好吧, bool 可以转换成Python对象,但这有点间接)。 Python本身仅在相当有限的情况下使用 __ bool __ (在Python 2中为 __ nonzero __ ),例如在 if 语句,而Cython通常遵循Python行为。因此,我将测试代码更改为

  1. I'm not convinced print(x) should convert it to bool anyway. print(x) looks for a conversion to a Python object (and OK, bool can be converted to a Python object, but it's somewhat indirect). Python itself uses the __bool__ (__nonzero__ in Python 2) only in fairly limited circumstances, like in an if statement, and Cython typically follows Python behaviour as a rule. Therefore I've changed the test-code to

def testit():
    cdef X x
    if x:
        print("is True")
    else:
        print("if False")


  • 运算符bool()给出错误


    'operator'不是类型标识符

    'operator' is not a type identifier

    我认为它需要像其他所有C ++一样以返回类型开头功能(即 operator 没有特殊情况)。这项工作(有点...见下一点...):

    I assumed it needed to start with the return type like every other C++ function (i.e. no special case for operator). This works (sort of... see next point...):

    bool operator bool()
    

    ,而这种语法就是在Cython的测试套件中进行了测试

    但是,您这样做需要从文件顶部的libcpp cimport bool 中进行以获得C ++ bool 类型。

    However, you do need to do from libcpp cimport bool at the top of the file to get the C++ bool type.

    如果您查看 if x的转换源: as

    __pyx_t_1 = __pyx_v_x.operator bool();
    if (__pyx_t_1) {
    

    操作员布尔被显式调用(对于Cython来说很常见),但是在正确的位置使用了$ c>,因此Cython清楚地了解了它的用途。同样,如果在没有定义运算符的情况下执行 if x:,则会出现错误

    operator bool is called explicitly (which is pretty common for Cython), but is used in the correct place so Cython clearly understands what it's for. Similarly, if you do if x: without defining the operator you get the error


    X类型的对象没有属性操作符布尔

    Object of type 'X' has no attribute 'operator bool'

    再次表明这是Cython的功能。

    again, suggesting this is a feature of Cython.

    这里显然存在一些文档故障,而且也许将来如果语法变得更接近C ++,我也不会100%感到惊讶。

    There's clearly a bit of a documentation failure here, and I wouldn't be 100% surprised if the syntax changed to match C++ a bit closer in the future, maybe.

    对于更一般的情况:看来, bool 是目前唯一受支持的类型转换运算符,因此您将无法定义其他运算符。

    For the more general case: it looks like bool is the only type-conversion operator supported at the moment, so you aren't able to define other operators.

    这篇关于Cython:如何使用用户定义的C ++类转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆