3

Can <initializer_list>s be directly declared for use in Cython constructors?

As I understand, this is possible:

# Cython
cdef int[3] li = [1, 2, 3]  

# C++
int[3] li = {1, 2, 3}

But similar syntax for the std::vector class like

cdef vector[int] * li = new vector[int]([1,2,3]) 

that uses the <initializer_list> constructor (6) seems unsupported by libcpp.vector.

Can one declare Cython constructors like the one above, in which [1, 2, 3] is interpreted as <initializer_list>? If so, how?

fhchl
  • 711
  • 8
  • 18
  • What is the error message you get? – ead Jul 24 '20 at 15:54
  • My question was about the possibility of declaring constructors that use initializer_list syntax (`{}`). I updated the question to clarify that. – fhchl Jul 25 '20 at 10:12
  • I'm pretty sure the answer is "no". Don't have any good suggestions for a workaround either – DavidW Jul 25 '20 at 19:02

1 Answers1

0

The documentation of Cython states that this syntax is not supported:

cdef int[3] li = [1, 2, 3]  

Please look at this section of the documentation:

def func():
    cdef int i = 10, j, k
    cdef float f = 2.5
    # cdef float g[4] = [1,2,3,4]  # currently not supported
    cdef float *g = [1, 2, 3, 4]
    cdef float *h = &f

Maybe this can help you...