使用函数和 Python 进行 Boost.Bind
我遇到一些编译时错误,但我不明白这是为什么。以下代码将拒绝编译,并给出以下错误:
错误 C2664:“void (PyObject *,const char *,boost::type *)”:无法将参数 1 从“const char *”转换为“PyObject *”
错误 C2664: 'void (PyObject *,const char *,boost::type *)': 无法将参数 3 从 'boost::shared_ptr' 转换为 'boost::type *'
PyObject* self = ...;
const char* fname = "...";
boost::function<void (boost::shared_ptr<Event>)> func;
func = boost::bind(boost::python::call_method<void>, self, fname, _1);
I get some compile time errors and I can't understand why that is. The following code will refuse to compile, giving me the following errors:
error C2664: 'void (PyObject *,const char *,boost::type *)' : cannot convert parameter 1 from 'const char *' to 'PyObject *'
error C2664: 'void (PyObject *,const char *,boost::type *)' : cannot convert parameter 3 from 'boost::shared_ptr' to 'boost::type *'
PyObject* self = ...;
const char* fname = "...";
boost::function<void (boost::shared_ptr<Event>)> func;
func = boost::bind(boost::python::call_method<void>, self, fname, _1);
boost::python::call_method
由几个带有不同数量参数的重载函数组成,定义如下:当您直接调用它时(例如
call_method(self, name, arg1, arg2)
),编译器可以自动选择正确的重载和模板化参数类型。但是,当您将指向call_method
的函数指针传递给bind
时,您需要手动指定重载和参数类型,方法是:或者在本例中:
boost::python::call_method
consists of several overloaded functions that take a different number of arguments, defined like this:When you call it directly (e.g.
call_method<void>(self, name, arg1, arg2)
), the compiler can choose the correct overload and templated argument types automatically. But when you pass a function pointer tocall_method
intobind
, you need to manually specify the overload and argument types, by using:Or in this case: