在Cython中,过载的operator()失效了。

问题描述 投票:0回答:1

我试图用Cython在Python中封装一个手写的模板化C++数组类。代码不能编译,因为 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
python c++ cython
1个回答
1
投票

我想 operator[] 已经被特殊处理过了,所以它在所谓的类似情况下也能用,这并不能说明什么。

我认为一般情况下,Cython会很难让这种语法发挥作用。它并不完全理解C++的引用,而且C++的调用经常使用临时变量(帮助包裹C++的异常处理),而这些引用肯定不能很好地工作。

你可能不得不放弃 "漂亮的" 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的docstring语法来包含小的CC++代码片段)

在你的Cython代码中,你可以这样做 setArrayValue(a, index, counter) 而不是。

© www.soinside.com 2019 - 2024. All rights reserved.