Integer template arguments?

836 views
Skip to first unread message

Ken

unread,
Aug 10, 2010, 7:20:11 PM8/10/10
to cython-users
Hi, I'm trying out the Cython 0.13b's C++ template capabilities, so I
thought I'd make a simple wrapper for Blitz++, which uses integer
values to specify number of dimensions for its various array and
vector types. Cython doesn't seem to understand integer template
arguments, though.

blitz.pxd:
cdef extern from "blitz/array.h" namespace "blitz":
cdef cppclass TinyVector[T,N]:
TinyVector()
TinyVector(T)
TinyVector(T,T)
TinyVector(T,T,T)
TinyVector(T,T,T,T) # etc
T& operator[](unsigned)

blitz.pyx:
cdef TinyVector[int,3] vec3


$ cython --cplus blitz.pyx

Error converting Pyrex file to C:
------------------------------------------------------------
...
cdef TinyVector[int,3] vec3
^
------------------------------------------------------------

blitz.pyx:1:20: unknown type in template argument

I tried "cdef cppclass TinyVector[T, int N]" and "cdef cppclass
TinyVector[T, cdef int N]", but neither parsed. Is there any way to do
this?

Robert Bradshaw

unread,
Aug 10, 2010, 7:24:51 PM8/10/10
to cython...@googlegroups.com
On Tue, Aug 10, 2010 at 4:20 PM, Ken <kwat...@gmail.com> wrote:
> Hi, I'm trying out the Cython 0.13b's C++ template capabilities, so I
> thought I'd make a simple wrapper for Blitz++, which uses integer
> values to specify number of dimensions for its various array and
> vector types. Cython doesn't seem to understand integer template
> arguments, though.z

That is correct, there is currently no support for integer template
arguments. You'll have to fake it with the cname way of doing things
(like had to be done with all C++ before 0.13).

- Robert

Chris Kees

unread,
Sep 27, 2010, 3:08:30 PM9/27/10
to cython-users
Could you point me to an example of the cname way of doing things? I'd
also like to wrap some simple template classes that have integer
template parameters. For example:

Test.h:
template<int shortLen>
class Test
{
...
};

test.pyx:

cdef extern from "Test.h":
cdef cppclass Test[shortLen]:
Test()
void calc(double* x, int longLen)

DEF shortLen=3
cdef Test[shortLen] t
x =numpy.zeros((10000,shortLen),"d")
t.calc(x.data,x.shape[0])

-Chris
On Aug 10, 6:24 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:
> On Tue, Aug 10, 2010 at 4:20 PM, Ken <kwatf...@gmail.com> wrote:
> > Hi, I'm trying out the Cython 0.13b's C++templatecapabilities, so I

Robert Bradshaw

unread,
Sep 27, 2010, 3:26:31 PM9/27/10
to cython...@googlegroups.com
On Mon, Sep 27, 2010 at 12:08 PM, Chris Kees <cek...@gmail.com> wrote:
> Could you point me to an example of the cname way of doing things? I'd
> also like to wrap some simple template classes that have integer
> template parameters.  For example:
>
> Test.h:
> template<int shortLen>
> class Test
> {
> ...
> };
>
> test.pyx:
>
> cdef extern from "Test.h":
>  cdef cppclass Test[shortLen]:
>    Test()
>    void calc(double* x, int longLen)
>
> DEF shortLen=3
> cdef Test[shortLen] t
> x =numpy.zeros((10000,shortLen),"d")
> t.calc(x.data,x.shape[0])

There's not a nice way of doing it, but here's the hacks that work:

cdef extern from *:
ctypedef int myIntParameter "xxx" # a fake type

cdef Test[myIntParameter] t

will create t of type Test<xxx>. You can also do

cdef extern fro "test.h":
cdef cppclass Test "Test<xxx>"

and treat Test as an unparameterized type to get the same result.

- Robert

Reply all
Reply to author
Forward
0 new messages