Way to handle integer template types

261 views
Skip to first unread message

csd

unread,
Mar 16, 2013, 6:56:02 AM3/16/13
to cython...@googlegroups.com
Hi all,

I am currently trying to wrap a C++ template class in Cython representing a sparse matrix. Here is a snippet of the C++ code:

template <class T, int S=Eigen::ColMajor>
class SparseMatrixExt:public SparseMatrix<T, S> {
...

Basically T is the data type of the elements of the matrix (int, float etc.) and S is an integer which dictates how entries are stored. When I try to wrap this in Cython I get the error "unknown type in template argument" referring to a fixed value of S. As far as I know, int template types are not yet supported, but what is the best way around this issue?

Thanks in advance,
Charanpal

Robert Bradshaw

unread,
Mar 16, 2013, 12:50:00 PM3/16/13
to cython...@googlegroups.com
You can do

cdef extern from *:
ctypedef void* int_parameter
int_parameter one "1"
int_parameter two "2"
int_parameter three "3"

cdef SparseMatrixExt<double, two> *m = new SparseMatrixExt<double, two>()

- Robert

Henry Schreiner

unread,
Apr 3, 2014, 2:03:33 PM4/3/14
to cython...@googlegroups.com
Are there any plans to add support for integer templates to cython? I'd like to have a general wrapper for std::array (would help with std::tuple, too).

Robert Bradshaw

unread,
Apr 3, 2014, 4:27:24 PM4/3/14
to cython...@googlegroups.com
On Thu, Apr 3, 2014 at 11:03 AM, Henry Schreiner
<henrysch...@gmail.com> wrote:
> Are there any plans to add support for integer templates to cython? I'd like
> to have a general wrapper for std::array (would help with std::tuple, too).

Not at the moment, though I can see how it would be useful. A
straw-man syntax could be something like CppClass[T, U, int N]?

> On Saturday, March 16, 2013 11:50:00 AM UTC-5, Robert Bradshaw wrote:
>>
>> On Sat, Mar 16, 2013 at 3:56 AM, csd <char...@gmail.com> wrote:
>> > Hi all,
>> >
>> > I am currently trying to wrap a C++ template class in Cython
>> > representing a
>> > sparse matrix. Here is a snippet of the C++ code:
>> >
>> > template <class T, int S=Eigen::ColMajor>
>> > class SparseMatrixExt:public SparseMatrix<T, S> {
>> > ...
>> >
>> > Basically T is the data type of the elements of the matrix (int, float
>> > etc.)
>> > and S is an integer which dictates how entries are stored. When I try to
>> > wrap this in Cython I get the error "unknown type in template argument"
>> > referring to a fixed value of S. As far as I know, int template types
>> > are
>> > not yet supported, but what is the best way around this issue?
>>
>> You can do
>>
>> cdef extern from *:
>> ctypedef void* int_parameter
>> int_parameter one "1"
>> int_parameter two "2"
>> int_parameter three "3"
>>
>> cdef SparseMatrixExt<double, two> *m = new SparseMatrixExt<double, two>()
>>
>> - Robert
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Henry Schreiner

unread,
Apr 4, 2014, 1:43:28 PM4/4/14
to cython...@googlegroups.com
Or CppClass[T, U, const int N]...

Moritz Kassner

unread,
Aug 20, 2015, 4:10:19 AM8/20/15
to cython-users
+1 on this.

wrapping libs that use Eigen would be so much more elegant with integer templates.

slonik

unread,
Aug 20, 2015, 10:12:44 AM8/20/15
to cython-users

+1 as well! Proper C++ template support would be a huge help for those of us who are using Cython with modern C++ libraries.

--Leo

 

Robert Bradshaw

unread,
Aug 20, 2015, 11:44:36 AM8/20/15
to cython...@googlegroups.com
Are you thinking of something like
https://github.com/cython/cython/pull/426 ? It still needs a bit of
work, but if you want this feature sooner you're welcome to help
review it yourself.

- Robert

Moritz Kassner

unread,
Aug 20, 2015, 12:29:23 PM8/20/15
to cython...@googlegroups.com
I d love to contribute back to the project which I use a lot a really appreciate! 
As a cython user I don't think I know enough to give useful review feedback :-(. 

Any other way to contribute and see this PR happen?

 

Reply all
Reply to author
Forward
0 new messages