There is currently a void in Cython's C++ support with respect to function (not
class) templates. It would be great to have such a thing, dangerous or not, so
I'm proposing something to get things rolling.

Given that function templates are 100% transparent to the caller, it seems that
the only barrier is Cython's type system. Even in the easiest case, where the
function returns a known primitive type for all input, we still can't use it.
    
    template<typename T>
    std::string to_string(T a)

    -------

    from libcpp.string import string as cpp_string

    cdef extern from "foo.h" namespace "std":

        cpp_string to_string(??? a, ??? b)
        

We can used fused types if we know that the function is restricted to numeric
types, for example, but in general this is not the case. The only workaround I
currently have is to declare the function N times for N types. This isn't
disastrous, but prevents sharing of code.

As an alternative, what about a dynamic ANY type that uses the fused type
machinery, but always succeeds when specializing? Or perhaps it just shouldn't
be type checked at all? There is always a backend that will generate the type
error and this possibly gives us macro "functions" for free in C.


    cdef extern from "foo.h" namespace "std":

        cpp_string to_string(cython.any_t a, cython.any_t b)


    Pros:
        Huge number of functions become accessible from Cython
        User explicitly states when a type should be unchecked
        Allows mixtures of typed and untyped parameters in a single call

    Cons:
        Makes determining return types hard in some cases.
        Error messages might be difficult to interpret
        ?????
        I'm-sure-this-list-should-be-longer


I'll admit I haven't dug very deep as far as the implications of such a thing.
Is it a reasonable idea? What are the major issues with such an approach?

-John
_______________________________________________
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel

Reply via email to