Cython malloc/new and templates

asked 2012-04-19 02:18:17 +0100

chaesloc2 gravatar image

updated 2012-04-19 14:01:13 +0100

%cython
#clang c++
from libcpp.vector cimport vector as cvector
from libcpp.list cimport list as clist
from cython.operator cimport dereference as deref, preincrement as inc
cdef extern from "stdlib.h":
    void free(void* ptr)
    void* malloc(size_t size)
    void* realloc(void* ptr, size_t size)

cdef cvector[clist[int]] *v
cdef clist[int] *l

v = new cvector[clist[int]]()
l = new clist[int]()

#v= < cvector[clist[int]] * > malloc(1*sizeof(cvector[clist[int]]))
#l= < clist[int] * > malloc(1*sizeof(clist[int]))



Error compiling Cython file:
------------------------------------------------------------
...

cdef cvector[clist[int]] *v
cdef clist[int] *l

v = new cvector[clist[int]]()
       ^
------------------------------------------------------------

_[...]_sage52_spyx_0.pyx:17:8: Operation only allowed in c++

EDIT: the problem below was addressed in the comments. (The problem above still stands)

Using malloc instead of new:

Error compiling Cython file:
------------------------------------------------------------
...

#v = new cvector[clist[int]]()
#l = new clist[int]()

v= < cvector[clist[int]] * > malloc(1*sizeof(cvector[clist[int]]))
                        ^
------------------------------------------------------------

_[...]_sage54_spyx_0.pyx:20:25: Unknown type
edit retag flag offensive close merge delete

Comments

The example in http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html in section "Templates" also fails with "Operation only allowed in c++". Sage 4.7.2.

chaesloc2 gravatar imagechaesloc2 ( 2012-04-19 02:32:36 +0100 )edit

Cast to template-dependent types is currently broken but I think fixed in Cython 0.16 (which will be out soon).

Volker Braun gravatar imageVolker Braun ( 2012-04-19 12:16:15 +0100 )edit

I upgraded to Cython 0.16 and the cast now works. Thanks.

chaesloc2 gravatar imagechaesloc2 ( 2012-04-19 13:54:47 +0100 )edit