Cython malloc/new and templates
%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
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.
Cast to template-dependent types is currently broken but I think fixed in Cython 0.16 (which will be out soon).
I upgraded to Cython 0.16 and the cast now works. Thanks.