Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping C++ template function/method in Cython

I'm trying to wrap some C++ Code with Cython. I have a class that utilizes a template method, but is not a template itself.

class SomeClass {
    template <class T> SomeClass(T& spam);
};

As the class is not a template but only the Constructor, I cannot declare the class as a template in Cython like this.

# wrong!
cdef extern from "SomeClass.h":
    cppclass SomeClass [T]:
        SomeClass(T& spam)

How can I wrap the template-method?

like image 453
Niklas R Avatar asked Jan 03 '12 16:01

Niklas R


1 Answers

For a non-constructor template method, using the following non-template class:

class SomeClass {
    template <class T> void other(T& spam);
};

I was able to get this to work:

cdef extern from "someclass.h":
    cppclass SomeClass:
        void other[T](T &spam)

That may not help you if you specifically need a constructor template method, but it does appear that Cython's support for template methods has improved at least slightly since the time when this question was originally asked.

like image 177
b4hand Avatar answered Nov 04 '22 23:11

b4hand