I recall somewhere saying that ROOT supports calling Python functions from C++ by converting functions into std::function. Is this information correct? If it is, how can I do so? My experiments kept failing.
I have tried passing a lambda as a std::function. It doesn’t work
>>> import ROOT
>>> gROOT = ROOT.gROOT
>>> gROOT.ProcessLine("int foo(function<int(int,int)> f) { return f(1, 1); }")
0
>>> ROOT.foo(lambda a, b: a+b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int ::foo(function<int(int,int)> f) =>
could not convert argument 1
Template also doesn’t work
>>> import ROOT
>>> gROOT = ROOT.gROOT
>>> gROOT.ProcessLine("template <typename T>int foo(const T& f) { return f(1, 1); }")
0
>>> ROOT.foo(lambda a, b: a+b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can not resolve method template call for 'foo'
Nor function pointers work
>>> import ROOT
>>> gROOT = ROOT.gROOT
>>> gROOT.ProcessLine("using func_t = int(int,int);")
0
>>> gROOT.ProcessLine("int foo(func_t f) { return f(1, 1); }")
0
>>> ROOT.foo(lambda a, b: a+b)
<stdin>:1: RuntimeWarning: creating converter for unknown type "int(int,int)"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int ::foo(int(int,int) f) =>
could not convert argument 1
What am I doing wrong?