Cython & C++: passing by reference

前端 未结 1 1387
温柔的废话
温柔的废话 2020-12-17 10:14

I am a noob with Cython and C++, so I have a question on argument passing. I want to avoid passing a copy of an argument in the following scenario:

# somefil         


        
相关标签:
1条回答
  • 2020-12-17 10:25

    Found the answer to my own question. Apparently, you can pass by reference, but the function MUST be cdef'ed, not def'ed. i.e.

    # somefile.pyx
    #distutils: language = c++
    from libcpp.vector cimport vector
    
    cdef void add_one(vector[int]& vect):
        cdef int i
        n = vect.size()
        for i in range(<int>n):
            vect[i] += 1
    
    cdef vector[int] v
    for i in range(100000):
        v.push_back(i)
    add_one(v)
    
    0 讨论(0)
提交回复
热议问题