wlav / cppyy

Other
384 stars 38 forks source link

Conversion operator seems not to be working #218

Open jmcarcell opened 4 months ago

jmcarcell commented 4 months ago

We found that the conversion operator seems not to be used when using python bindings from cppyy in https://github.com/key4hep/EDM4hep/issues/270. The following example from @m-fila shows that:

>>> import cppyy
>>> import cppyy.ll
>>> cppyy.__version__
'3.1.2'
>>> cppyy.cppdef("""
... 
... namespace ns {
... struct To{};
... 
... struct From{
...     operator To() const {return To();} 
... };
... 
... int foo(const To&) {return 0;}
... 
... }""")
True
>>> f = cppyy.gbl.ns.From()
>>> foo = cppyy.gbl.ns.foo
>>> foo(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int ns::foo(const ns::To&) =>
    TypeError: could not convert argument 1

Is this supported in cppyy? The workaround is to do the casting as shown in https://github.com/key4hep/EDM4hep/issues/270 with cppyy.ll.cast or cppyy.ll.static_cast but it makes the syntax a bit ugly.

wlav commented 4 months ago

That is indeed currently not supported. Type casting really isn't a thing in Python and although copy construction is supported, it's really a very inefficient path (much less efficient than being explicit about). Same goes for here if an implementation were attempted: the method (and all its overloads) would first need to fail, then a search for a suitable conversion operator would need to start, with the caveat that in Python, by-value, by-ref, by-pointer, and by-move are all plausible candidates. Then match and try again. All not impossible, just very inefficient compared to usingll.cast.