Boost.Bind与Function和Python

时间:2011-08-03 14:13:41

标签: c++ boost-python boost-bind boost-function

我得到一些编译时错误,我无法理解为什么会这样。以下代码将拒绝编译,给我以下错误:

  

错误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);

1 个答案:

答案 0 :(得分:1)

boost::python::call_method由几个重载函数组成,它们使用不同数量的参数,定义如下:

template <class R>
R call_method(PyObject* self, char const* method);
template <class R, class A1>
R call_method(PyObject* self, char const* method, A1 const&);
template <class R, class A1, class A2>
R call_method(PyObject* self, char const* method, A1 const&, A2 const&);
...

当你直接调用它时(例如call_method<void>(self, name, arg1, arg2)),编译器可以自动选择正确的重载和模板化参数类型。但是当您将函数指针传递给call_methodbind时,您需要使用以下命令手动指定重载和参数类型:

call_method<ReturnType, Arg1Type, Arg2Type, ...>

或者在这种情况下:

call_method<void, boost::shared_ptr<Event> >
相关问题