1

I have the following function signature that I am trying to wrap with Boost. Python.

void myfunc(std::string str1, std::string str2,
            std::function<void(std::string)> callback1,
            std::function<void(int, std::string)> callback2);

I tried exporting it in Boost.Python as:

BOOST_PYTHON_MODULE(my_module)
{
    using namespace boost::python;

    class_<MyClass>("MyClass")
        .def("myfunc", &MyClass::myfunc)
    ;
}

And calling it in Python with:

def update(message):
    print(message)


def error(status, message):
    print("Error {}: {}".format(status, message))


strarg1c= r"""path..."""
strarg2 = "test"

import my_module
obj = my_module.MyClass()
obj.myfunc(strarg1, strarg2, update, error)

But I get the following errors:

Boost.Python.ArgumentError: Python argument types in
    MyClass.myfunc(MyClass, str, str, function, function)
did not match C++ signature:
    myfunc(class Myclass {lvalue}, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, class std::function<void __cdecl(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)>, class std::function<void __cdecl(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)>)

I've found some similar questions but they generally deal with handling arbitrary functions as return types and not one in a list of arguments to a function. I'm hoping there is something simple I am missing when it comes to Boost.Python and std::function.

It seems Pybind11 has this functionality. Is this functionality missing in Boost.Python or am I missing something?

nalyd88
  • 4,940
  • 8
  • 35
  • 51

0 Answers0