title: Cython: How to use templates?
body: <p>I would like to cythonize the following templated C++ class:</p>
  
  <pre><code>template &lt;typname T&gt;
  class Fc2Par
  {
  public:
    Fc2Par(std::string const&amp; par_file)
    ~Fc2Par()
    std::vector&lt;Box&lt;T&gt;&gt; convert_boxes(std::vector&lt;Box&lt;T&gt;&gt; const&amp; boxes) const;
    std::vector&lt;Point&lt;T&gt;&gt; convert_points(std::vector&lt;Point&lt;T&gt;&gt; const&amp; points) const;
  private:
    PartitionMap&lt;T&gt;  par_map;
    PartitionRTree&lt;T&gt; par_idx;
  };
  </code></pre>
  
  <p>In reality, T will be [int, double] only. Box/Point are additional templated classes but I'm not sure if i want to expose that in python. To cythonize, I have the following, but I'm stuck in some areas. I think I can use a fused type for T?</p>
  
  <pre><code>cimport cython
  from libcpp.vector cimport vector
  from libcpp.string cimport string
  
  my_fused_type = cython.fused_type(cython.int, cython.double)
  
  cdef extern from 'Fc2Par.h':
    cdef cppclass Fc2Par[T]
      Fc2Par(string&amp;) except +
      vector[Box[T]] convert_boxes(vector[Box[T]]&amp;)
      vector[Point[T]] convert_points(vector[Point[T]]&amp;)
  
  cdef class PyFc2Par:
    cdef Fc2Par* thisptr &lt;-- should this be Fc2Par[my_fused_type]*?
  
    def __cinit__(self, par_file):
      self.thisptr = new Fc2Par[my_fused_type](par_file)
    def __dealloc__(self)
      del self.thisptr
    def convert_boxes(self, boxes)
  
      I'm not sure what to do here?
  
    def convert_points(self, points)
  
      This will be very similar to convert_boxes once I figure that out.
  </code></pre>
  
  <p>Ideally, I want to use the API in python like this:</p>
  
  <pre><code>boxes_int = [(0,0,1,1), (0,0,2,2), ...]
  boxes_float = [(0.0,0.0,1.0,1.0), (0.0,0.0,2.0,2.0), ...]
  
  fc2par = PyFc2Par('foo.csv')
  converted_int = fc2par.convert_boxes(boxes_int)
  converted_float = fc2par.convert_boxes(boxes_float)
  </code></pre>
  
  <p>They return a list of tuples with xmin,xmax,ymin,ymax.</p>
  
  <p><strong>My Questions:</strong></p>
  
  <ol>
  <li><p>Is using a fused type correct in this situation?</p></li>
  <li><p>If I take a list of tuples, how do I convert them into Box[T]/Point[T] in the Cython code without exposing these classes in Python? Once I have the result, I can convert that back to a list of tuples and send that back. i.e., how should the convert_boxes implementation look like? </p></li>
  </ol>
  
  <p>Thank you for any help.</p>