C ++ template function / method wrap in Cython

I am trying to build C ++ code with Cython. I have a class that uses the template method, but is not the template itself.

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

Since the class is not a template, but only a 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 a template method?

+7
source share
3 answers

Easy. (Or I think it is). In the C ++ class, the element is templated, whereas in Cython you declare a template for templates. Change your code to:

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

If possible, or:

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

If Cython supports it.

I am not an expert in cython, so I could be wrong.

0
source

How about Boost Fairing for cython?

http://www.boost.org/doc/libs/1_54_0/libs/python/doc/index.html

Welcome to version 2 of the Boost.Python library, C ++, which provides seamless interoperability between C ++ and the Python programming language. The new version has been rewritten from scratch, with a more convenient and flexible interface and many new features, including support for: Links and pointers Influences around the world Automatic cross-module conversions Effective function overloading Translating C ++ scripts to Python Default arguments Keyword arguments Manipulating Python Objects in C ++ Exporting C ++ Iterators as Python Iterators Documentation Lines

I assume that you are looking for something like this, it already exists as part of the C ++ boost library.

0
source

For the nonconstructor template method, use the following class without a template:

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

I managed to get this to work:

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

This may not help you if you definitely need a constructor template method, but it looks like Cython support for template methods has improved, at least since this question was asked.

0
source

All Articles