重载的 operator() 在 Cython 中失败
Overloaded operator() is failing in Cython
我正在尝试使用Cython将手写的模板化C++数组类包装在Python中。代码无法编译,因为应返回引用的operator()
在编译期间触发错误:Cannot assign to or delete this
。如何编译我的代码?
我的.pyx
文件是:
# distutils: language = c++
import cython
import numpy as np
cimport numpy as np
cdef extern from "<array>" namespace "std":
cdef cppclass Dim_array_2 "std::array<int, 2>":
Dim_array_2() except+
int& operator[](size_t)
cdef extern from "Array.h":
cdef cppclass Array_2 "Array<double>":
Array_2() except +
void set_dims(const Dim_array_2&) except +
double& operator()(const Dim_array_2&)
def print_index():
cdef Dim_array_2 dims
dims[0] = 3
dims[1] = 5
cdef Array_2 a
a.set_dims(dims)
cdef Dim_array_2 index
counter = 0.
for j in range(dims[1]):
for i in range(dims[0]):
index[0] = i+1
index[1] = j+1
a(index) = counter # This fails.
counter += 1.
index[0] = 2
index[1] = 3
print(a(index))
相应C++Array.h
的简化版本为:
#include <array>
#include <vector>
template<typename TF>
class Array
{
public:
Array() {}
void set_dims(const std::array<int,2>& dims)
{
data.resize(dims[0]*dims[1]);
this->dims = dims;
}
TF& data operator()(const std::array<int, 2>& index)
{
return data[ (index[0]-1) + (index[1]-1)*dims[0] ];
}
private:
std::vector<TF> data;
std::array<int,2> dims;
};
完整的错误消息是:
Error compiling Cython file:
------------------------------------------------------------
...
counter = 0.
for j in range(dims[1]):
for i in range(dims[0]):
index[0] = i+1
index[1] = j+1
a(index) = counter # This fails.
^
------------------------------------------------------------
test.pyx:31:13: Cannot assign to or delete this
我认为operator[]
是专门针对特殊情况工作的,因此它在假定相似的情况下工作的事实并不能告诉您太多信息。
我认为总的来说,Cython 将努力使这种语法起作用。它不完全理解C++引用,并且C++调用通常使用临时变量(以帮助包装异常处理C++(,这些变量绝对不适用于引用。
您可能不得不放弃"nice"array(index) = value
语法,并编写一个小的C++包装器函数来帮助您:
cdef extern from "Array.h":
"""
void setArrayValue(Array<double>& arr, const std::array<int, 2>& idx, double value) {
arr(idx) = value;
}
"""
# ...
(我使用Cython的文档字符串语法来包含小的C/C++代码片段。
在你的Cython代码中,你会做setArrayValue(a, index, counter)
。
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- 重载的 operator() 在 Cython 中失败
- 将 .c 文件重命名为 .cpp,导入 Cython 库失败
- Cython c++示例失败了,原因是什么
- C++API声明与Cython一起失败