【问题标题】:Why can't std::ref be used to pass objects into Boost.Python modules?为什么不能使用 std::ref 将对象传递给 Boost.Python 模块?
【发布时间】:2016-06-29 17:59:22
【问题描述】:

环境:使用 Python 3.5 编译的 Boost 1.61.0

以下 C++ 代码输出12

class A
{
public:
    int func() { return 12; }
};

BOOST_PYTHON_MODULE(bridge)
{
    using namespace boost::python;
    class_<A>("A", no_init)
        .def("func", &A::func);
}

int main()
{
    A a;
    PyImport_AppendInittab("bridge", PyInit_bridge);
    Py_Initialize();
    using namespace boost::python;

    dict dictMain = extract<dict>(import("__main__").attr("__dict__"));

    import("bridge").attr("a") = boost::ref(a);
    exec("import bridge", dictMain);
    exec("print(bridge.a.func())", dictMain);
}

但是,如果我将 boost::ref 替换为 std::ref,则会引发 boost::python::error_already_set 实例。

为什么这里不能使用std::ref

【问题讨论】:

    标签: python c++ boost boost-python boost-ref


    【解决方案1】:

    关于handling python exceptions in C++ 的好文章。由于stdboost 库中reference_wrapper 实现的差异,我通过异常AttributeError 猜测python 的任何方式。即使在公共界面中,您也可以看到差异。 std::reference_wrapper 没有 get_pointer() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      相关资源
      最近更新 更多