Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Specify C++ template type in Python
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Robert Bradshaw  
View profile  
 More options Mar 23 2012, 5:07 pm
From: Robert Bradshaw <rober...@gmail.com>
Date: Fri, 23 Mar 2012 14:07:59 -0700
Local: Fri, Mar 23 2012 5:07 pm
Subject: Re: [cython-users] Specify C++ template type in Python

On Fri, Mar 23, 2012 at 4:44 AM, csd <charan...@gmail.com> wrote:
> I wanted to wrap some C++ code using Cython (to be callable in Python)
> and wondered if one could specify the template type in Python itself
> somehow. To illustrate the problem, consider the code which wraps the C
> ++ vector class in Cython (taken from http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html
> )

> from cython.operator cimport dereference as deref, preincrement as inc
> #dereference and increment operators

> cdef extern from "<vector>" namespace "std":
>    cdef cppclass vector[T]:
>        cppclass iterator:
>            T operator*()
>            iterator operator++()
>            bint operator==(iterator)
>            bint operator!=(iterator)
>        vector()
>        void push_back(T&)
>        T& operator[](int)
>        T& at(int)
>        iterator begin()
>        iterator end()

> Ideally I would like to make this code callable from Python using
> something like:

> a = PyVector(int)

> where int becomes the template type.

> My incorrect attempt so far looks like this:

> cdef class PyVector:
>    cdef vector[T] *thisptr      # hold a C++ instance which we're
> wrapping
>    def __cinit__(self, tempType):
>        self.thisptr = new vector[tempType]()

> but I need to specify the template type of the instance variable
> before the constructor. One way to get around it is to hard-code
> several types in different classes, and then choose one in the
> constructor of PyVector, but this isn't ideal. Any ideas?

No, this isn't (currently) possible. The issue is that the C++
template type needs to be determined at compile time, but you're
trying to defer the decision to runtime. Compiling several different
specializations is the only way to do it.

You may also want to take a look at fused types (though this is not
yet supported for classes).

- Robert


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.