Cython 导入 C++ 运算符:"Overloading operator '+=' not yet supported."
Cython importing C++ operators: "Overloading operator '+=' not yet supported."
我正在尝试使用 MPFR C++ 库,因为我想为 MPFR 数字使用简单的运算符(例如“+=”或什至只是“+”)。
我的简单例子如下
cdef extern from "mpreal.h" namespace "mpfr":
cdef cppclass mpreal:
mpreal() except +
mpreal(const double) except +
mpreal& operator+=(const mpreal& v) except +
但是,Cython 抱怨
Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "mpreal.h" namespace "mpfr":
cdef cppclass mpreal:
mpreal() except +
mpreal(const double) except +
mpreal& operator+=(const mpreal& v) except +
^
------------------------------------------------------------
test_000.pyx:6:26: Overloading operator '+=' not yet supported.
https://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html 中提到的一些运算符的语法略有不同,但是“+=”运算符在互联网上的任何地方都没有提到,同时相当简单和常见,所以我'我很困惑。
我的问题相当愚蠢:我是不是在某处犯了错误,或者错误消息是否正确,因为 Cython 尚不支持“+=”运算符? “=”等其他运算符(似乎)有效。
编辑: 用户 ead 在 Cython (https://github.com/cython/cython/blob/5a8c984403ce73ac29f9e8b7c78ee5f0f4c608a3/Cython/Compiler/Parsing.py#L2834) 的 src 中指出了一些内容,我理解为不支持“+=”。
但是看着 https://github.com/cython/cython/blob/5a8c984403ce73ac29f9e8b7c78ee5f0f4c608a3/Cython/Compiler/Parsing.py#L2909 我想知道这是否可以轻松扩展并且代码注释实际上暗示了这一点?也许我可以自己弄清楚并做出贡献,但如果有人知道发生了什么,我会很高兴听到它。
elif s.sy == '=':
op += s.sy # +=, -=, ...
s.next()
if op not in supported_overloaded_operators:
s.error("Overloading operator '%s' not yet supported." % op,
fatal=False)
重要编辑:
讨论也在 https://github.com/cython/cython/issues/3696 上进行,现在看来,ead 的建议将是 added/fixed 在 Cython 中。
对我来说,运算符 +=
似乎被遗忘了:+=
(以及 -=
、*=
、/=
)没有什么不同比正常的赋值运算符 =
.
当我将枚举支持的运算符的 this line in Cython-code 更改为
时
supported_overloaded_operators = cython.declare(set, set([
'+', '-', '*', '/', '%',
'++', '--', '~', '|', '&', '^', '<<', '>>', ',',
'==', '!=', '>=', '>', '<=', '<',
'[]', '()', '!', '=',
'bool', '+=', #`+=` added!
]))
我可以成功构建和使用以下简约复制器:
%%cython -+ -c=-std=c++11
cdef extern from *:
"""
struct A{
int x=42;
A& operator+=(const A&o){
x+=o.x;
}
};
"""
cdef cppclass A:
int x
A& operator+=(const A& v)
def doit():
cdef A a;
a+=a;
print(a.x)
现在:
doit() # prints 84
完成任务。
我正在尝试使用 MPFR C++ 库,因为我想为 MPFR 数字使用简单的运算符(例如“+=”或什至只是“+”)。
我的简单例子如下
cdef extern from "mpreal.h" namespace "mpfr":
cdef cppclass mpreal:
mpreal() except +
mpreal(const double) except +
mpreal& operator+=(const mpreal& v) except +
但是,Cython 抱怨
Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "mpreal.h" namespace "mpfr":
cdef cppclass mpreal:
mpreal() except +
mpreal(const double) except +
mpreal& operator+=(const mpreal& v) except +
^
------------------------------------------------------------
test_000.pyx:6:26: Overloading operator '+=' not yet supported.
https://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html 中提到的一些运算符的语法略有不同,但是“+=”运算符在互联网上的任何地方都没有提到,同时相当简单和常见,所以我'我很困惑。
我的问题相当愚蠢:我是不是在某处犯了错误,或者错误消息是否正确,因为 Cython 尚不支持“+=”运算符? “=”等其他运算符(似乎)有效。
编辑: 用户 ead 在 Cython (https://github.com/cython/cython/blob/5a8c984403ce73ac29f9e8b7c78ee5f0f4c608a3/Cython/Compiler/Parsing.py#L2834) 的 src 中指出了一些内容,我理解为不支持“+=”。
但是看着 https://github.com/cython/cython/blob/5a8c984403ce73ac29f9e8b7c78ee5f0f4c608a3/Cython/Compiler/Parsing.py#L2909 我想知道这是否可以轻松扩展并且代码注释实际上暗示了这一点?也许我可以自己弄清楚并做出贡献,但如果有人知道发生了什么,我会很高兴听到它。
elif s.sy == '=':
op += s.sy # +=, -=, ...
s.next()
if op not in supported_overloaded_operators:
s.error("Overloading operator '%s' not yet supported." % op,
fatal=False)
重要编辑: 讨论也在 https://github.com/cython/cython/issues/3696 上进行,现在看来,ead 的建议将是 added/fixed 在 Cython 中。
对我来说,运算符 +=
似乎被遗忘了:+=
(以及 -=
、*=
、/=
)没有什么不同比正常的赋值运算符 =
.
当我将枚举支持的运算符的 this line in Cython-code 更改为
时supported_overloaded_operators = cython.declare(set, set([
'+', '-', '*', '/', '%',
'++', '--', '~', '|', '&', '^', '<<', '>>', ',',
'==', '!=', '>=', '>', '<=', '<',
'[]', '()', '!', '=',
'bool', '+=', #`+=` added!
]))
我可以成功构建和使用以下简约复制器:
%%cython -+ -c=-std=c++11
cdef extern from *:
"""
struct A{
int x=42;
A& operator+=(const A&o){
x+=o.x;
}
};
"""
cdef cppclass A:
int x
A& operator+=(const A& v)
def doit():
cdef A a;
a+=a;
print(a.x)
现在:
doit() # prints 84
完成任务。