I am using cppyy. I want to pass a function to my C++. The C++ expects a function of signature std::function<double(std::vector<double>)>
. I don't know how to do it. Here is a minimal example of the error:
import cppyy
# To be passed to C
def callback(x):
return 10.
cppyy.cppdef("double callback_vector(const std::function<double(std::vector<double>)>& callback, std::vector<double> x) { return callback(x); }")
cppyy.cppdef("double callback(const std::function<double(double)>& callback, std::vector<double> x) { return callback(x[0]); }")
cppyy.gbl.callback(callback, [1]) # works
cppyy.gbl.callback_vector(callback, [1]) # fails
but I find
Traceback (most recent call last):
File "test.py", line 11, in <module>
cppyy.gbl.callback_vector(callback, [1])
TypeError: double ::callback_vector(function<double(std::vector<double> >& callback, vector<double> x) =>
TypeError: could not convert argument 1
In other words, with std::function<double(std::vector<double>)>
it fails, but with the simpler std::function<double(double)>
signature it works.