[Cython] Templated attributes in extensions classes in 0.17 Beta 2

Brett Calcott brett.calcott at gmail.com
Mon Aug 20 08:23:22 CEST 2012


I'm not sure if this is a bug in 0.17 beta or not. But something is not working.

Default-constructed attributes in an extension definition appear to
work (though I can't see where they are documented on the site…)

So this works:

----test.h --------
#include <vector>

struct Attributes
{
    std::vector<int> v;
};

-----test.pyx------
from libcpp.vector cimport vector

cdef extern from "test.h":
    cdef cppclass Attributes:
        vector[int] v

cdef class Test:
    cdef Attributes a
    def __cinit__(self, list x):
        self.a.v = x


However, if I try to put the templated attribute in directly:

----test2.pyx------
from libcpp.vector cimport vector

cdef class Test:
    cdef vector[int] v
    def __cinit__(self, list x):
        self.v = x

cython compiles it fine, but I get an error in the cpp compilation:
test2.cpp:722:23: error: expected a type
  new((void*)&(p->v)) std::vector();

So it looks like it's doing the funky in place construction, but not
getting the template parameters in there…

Should this work?

Cheers,
Brett


More information about the cython-devel mailing list