[Cython] Template functions

John Tyree johntyree at gmail.com
Sun Mar 17 20:15:17 CET 2013


> 
> I was thinking of something along the lines of
> 
> cdef extern from ...:
>     cpp_string to_string[T](T value)
>     T my_func[T, S](T a, S b)
>     ...
> 
> It's more a question of how to implement it.
> 
> - Robert

Well this closely matches the syntax used for classes and won't require any type
inference, since the user supplies the type at the call site (am I reading that
correctly?) so I'm not sure what about it will be particularly challenging.

If it's done this way the compiler could generate prototypes as necessary in a
preprocessing step, without inferring anything about the types until later when
overloading is resolved. That feels kind of hacky to me, but I've never written
a compiler with the size and scope of Cython, maybe it's not too bad.  This is
essentially what the user has to do already, and it "works".

The biggest complaint I have about this method is that without inference it
looks like it could lead to a *lot* of extra writing out of types. I'm dreading
the thought of writing out nested template types when calling factory functions
like those in the thrust library, which was what motivated this in the first
place.

-John


More information about the cython-devel mailing list