Unable to deduce type parameters in global function

36 views
Skip to first unread message

Marcel Martin

unread,
Jul 1, 2015, 5:48:02 AM7/1/15
to cython...@googlegroups.com
Hi,

I’m trying to wrap a heavily templatized C++ library that uses classes
and global functions acting on them.

I would like to know why, in the following example, the last line gives
me the error 'Unable to deduce type parameters' whereas I get no
complaints about the line preceding it:

cdef extern from "file.h":
cdef cppclass Container[T]:
pass

T get_first[T](Container[T])
T get[T](Container[T], size_t)

cdef Container[int] c
get_first(c)
get(c, 1)

I’m just starting to use templates in Cython, so I wonder if I’m doing
something wrong or whether this is just not supported.

Regards,
Marcel

David Vierra

unread,
Jul 4, 2015, 4:44:00 PM7/4/15
to cython...@googlegroups.com
Cython is unable to deduce the type parameters for that function call because the type of the second argument is different (size_t vs long). It should implicitly convert the argument as C++ does, but it looks like a bug makes it abort template deduction when the type of the non-template argument doesn't match.

`get(c, <size_t>1)` ought to work.

--

Details:

In best_type(), PyrexTypes.py:3991, it calls `pattern.type.deduce_template_params(actual)`, where `pattern.type` is `size_t` and `actual` is `long`. The call to `deduce_template_params` calls the function of BaseType, which returns None if the two types are not exactly equal, which gets passed through merge_template_deductions and triggers the `if deductions is None:` on the next line.

Maybe `BaseType.deduce_template_params` should return {} if the two types are convertible, since later on in `best_type()`, the (overloaded) candidate functions are ranked by implicit conversions.

Marcel Martin

unread,
Jul 6, 2015, 4:15:49 AM7/6/15
to cython...@googlegroups.com
On 2015-07-04 14:52, David Vierra wrote:
> Cython is unable to deduce the type parameters for that function call
> because the type of the second argument is different (size_t vs long).
> It should implicitly convert the argument as C++ does, but it looks like
> a bug makes it abort template deduction when the type of the
> non-template argument doesn't match.
>
> `get(c, <size_t>1)` ought to work.

Hi David,

that did it! Thanks for investigating this. I have in the meantime
posted the question on Stackoverflow, perhaps you would like to copy
your answer there:
http://stackoverflow.com/questions/31201382/

I saw just now a comment there that specifying the type parameter
explicitly as in 'get[int](c, 1)' also works. It does, but in reality, I
have multiple type parameters and I’d like to avoid having to type it
all out, so I prefer your suggestion.

> Details:
>
> In best_type(), PyrexTypes.py:3991, it calls
> `pattern.type.deduce_template_params(actual)`, where `pattern.type` is
> `size_t` and `actual` is `long`. The call to `deduce_template_params`
> calls the function of BaseType, which returns None if the two types are
> not exactly equal, which gets passed through merge_template_deductions
> and triggers the `if deductions is None:` on the next line.
>
> Maybe `BaseType.deduce_template_params` should return {} if the two
> types are convertible, since later on in `best_type()`, the (overloaded)
> candidate functions are ranked by implicit conversions.

I see: When I temporarily change the size_t in the declaration of get()
to long, Cython also accepts it. I have looked at the code now for a
while, but I cannot propose a patch myself, so perhaps one of the
developers has time.

Thanks again,
Marcel



Reply all
Reply to author
Forward
0 new messages