Wrap C++ template function with multiple template parameter and typdef for specific instantiations of the typdef

989 views
Skip to first unread message

Spencer Lyon

unread,
Jun 27, 2013, 12:42:50 PM6/27/13
to cython...@googlegroups.com
I know there have been similar questions asked, but I haven't been able to see anything that addresses my problem specifically. 

I have some C++ code that looks something like this:

template< uint S, typename T = double, bool G = false >
class Point {};

template< typename T >
class Point< 3, T, true >
{

public:

/// Constructor to copy the elements from an array of size 4.
template< typename T2 >
Point( const T2 array[ 4 ] )
: x( static_cast< T >( array[X] ) ),
y( static_cast< T >( array[Y] ) ),
z( static_cast< T >( array[Z] ) ),
w( static_cast< T >( array[ W3D ] ) )
{
}

/// Constructor to set the Point components with explicit values.  Note
/// that T2 must be able to be safely cast to type T.
template< typename T2, typename T3, typename T4, typename T5 >
Point( const T2& x, const T3& y, const T4& z, const T5& w )
: x( static_cast< T >( x ) ),
y( static_cast< T >( y ) ),
z( static_cast< T >( z ) ),
w( static_cast< T >( w ) )
{
}

                // About 20 more methods... 

        };

typedef Point< 3, double, true > Point3d;


My goal is to be able to use the Point3d typedef from python (in fact, the C++ code only uses that typedef when dealing with that particular template: Point< 3, double, true>).

If there is an easy way to expose this typedef to python via cython? If so, that would be great. If not, how would I write my own wrapper for the Point class with the template <3, double, true>?

Previous questions, as well as parts of the cython documentation, deal with very simple C++ templates; this one isn't quite as straightforward. 

I realize this question may not be well-developed, but if anyone things they might be able to help me out, let me know what other information you need and I would be happy to give it to you.

Thanks,

Spencer

Robert Bradshaw

unread,
Jun 27, 2013, 1:19:47 PM6/27/13
to cython...@googlegroups.com
Cython doesn't yet have support for templatized functions or non-type
template parameters. You could just declare Point3d as a class with
the relevant methods.

- Robert

Spencer Lyon

unread,
Jun 27, 2013, 1:40:13 PM6/27/13
to cython...@googlegroups.com
Thanks for the quick response.

I don't think I understand what you mean by "Cython doesn't yet have support for templatized functions ... ". Could you elaborate?

Also what would I need to do to declare a Point3d class that accesses the methods and class definition of Point< 3, double, true>?

Spencer Lyon

unread,
Jun 27, 2013, 1:41:44 PM6/27/13
to cython...@googlegroups.com
Oh and sorry I meant access the class definition Point< 3, T, true>, where T = double (like in the Point3d typedef)

Robert Bradshaw

unread,
Jun 27, 2013, 1:49:49 PM6/27/13
to cython...@googlegroups.com
On Thu, Jun 27, 2013 at 10:40 AM, Spencer Lyon <spence...@gmail.com> wrote:
> Thanks for the quick response.
>
> I don't think I understand what you mean by "Cython doesn't yet have support
> for templatized functions ... ". Could you elaborate?

Your constructor is templatized on the type. You'd declare an overload
for each specific type you want to use.

> Also what would I need to do to declare a Point3d class that accesses the
> methods and class definition of Point< 3, double, true>?

I don't know what the methods are, but suppose you had a generic T
getCoordinate(int index) method. Then you'd declare

cdef extern from ...:
cdef cppclass Point3d:
...
cdef double getCoordinate(int)

This is assuming you have "typedef Point< 3, double, true > Point3d"
in your included files. You could also write

cdef cppclass Point3d "Point<3, double, true>":

and avoid the external typedef. You can also declare fake types

cdef extern from *:
ctypedef int THREE "3"
ctypedef int TRUE "true"

Then write

cdef cppclass Point[SIZE, T, BOOL]:
...

ctypedef Point[THREE, double, TRUE] Point3d
> --
>
> ---
> 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/groups/opt_out.
>
>

Anthony Scopatz

unread,
Jul 7, 2013, 5:13:54 PM7/7/13
to cython...@googlegroups.com
Hi Robert, 

Thanks for posting this.  The cdef cppclass Point3d "Point<3, double, true>" syntax was especially helpful for xdress.  

Would you essentially do the same thing for templated functions (not methods)?  That is:

cdef int myfunc3d(double x, int y) "int myfunc<3, double, true>(double x, int y)":

Thanks again!

Be Well
Anthony

Bradley Froehle

unread,
Jul 8, 2013, 2:22:01 AM7/8/13
to cython...@googlegroups.com
Hi Anthony:


On Sunday, July 7, 2013 2:13:54 PM UTC-7, Anthony Scopatz wrote:
Hi Robert, 

Thanks for posting this.  The cdef cppclass Point3d "Point<3, double, true>" syntax was especially helpful for xdress.  

Would you essentially do the same thing for templated functions (not methods)?  That is:

cdef int myfunc3d(double x, int y) "int myfunc<3, double, true>(double x, int y)":

You should be able to do something like:

cdef extern from ...:
    int myfunc3d "myfunc<3, double, true>" (double x, int y)

Cython essentially pastes in whatever is in the quotes when referring to the function.

-Brad

Anthony Scopatz

unread,
Jul 8, 2013, 10:49:48 AM7/8/13
to cython...@googlegroups.com
Thanks for the tip, Brad.  

I'll give that a shot.

Be Well
Anthony


--
 
---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/JVzbh2Vbm60/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.

Anthony Scopatz

unread,
Jul 23, 2013, 11:54:36 PM7/23/13
to cython...@googlegroups.com
Thanks again Brad.

That seems to work!

Be Well
Anthony
To unsubscribe from this group and all its topics, send an email to cython-users+unsubscribe@googlegroups.com.

Jean-Pierre Flori

unread,
Sep 24, 2013, 9:28:44 AM9/24/13
to cython...@googlegroups.com
Hey all,

Actually I would also really love to get templated functions support in Cython to link Mathemagix which makes heavy use of such construction into Sage.
Is there any plan to do so in the near/distant future?

Best,
JP
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages