【问题标题】:Boost.Bind with Function and PythonBoost.Bind 与函数和 Python
【发布时间】:2011-08-03 14:13:41
【问题描述】:

我得到一些编译时错误,我不明白为什么会这样。以下代码将拒绝编译,给我以下错误:

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

【问题讨论】:

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


    【解决方案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&lt;void&gt;(self, name, arg1, arg2)),编译器可以自动选择正确的重载和模板化参数类型。但是,当您将指向call_method 的函数指针传递给bind 时,您需要手动指定重载和参数类型,方法是:

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

    或者在这种情况下:

    call_method<void, boost::shared_ptr<Event> >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      • 2012-11-10
      相关资源
      最近更新 更多