Using Boost Python & std::shared_ptr

前端 未结 4 2133
悲哀的现实
悲哀的现实 2021-02-13 06:30

I\'m trying to get Boost Python to play nicely with std::shared_ptr. Currently, I\'m receiving this error:

Traceback (most recent call last):
  File \"test.py\",         


        
相关标签:
4条回答
  • 2021-02-13 06:47

    Unless I've misunderstood, I think this solves your problem:

    boost::python::register_ptr_to_python<std::shared_ptr<Anchor>>();
    

    http://www.boost.org/doc/libs/1_57_0/libs/python/doc/v2/register_ptr_to_python.html

    0 讨论(0)
  • 2021-02-13 06:55

    Looks like boost::python doesn't support C++ 11 std::shared_ptr.

    If you have a look to file boost/python/converter/shared_ptr_to_python.hpp you'll find implementation of template function shared_ptr_to_python(shared_ptr<T> const& x) for boost::shared_ptr (it explain why the code works fine for boost::shared_ptr).

    I think you have several options:

    • use boost::shared_ptr (which you trying to avoid)
    • write your implementation of shared_ptr_to_python for std::shared_ptr (IMHO the best option)
    • send request to boost::python developers to support std::shared_ptr
    0 讨论(0)
  • 2021-02-13 07:03

    There's a bug report for this: https://svn.boost.org/trac/boost/ticket/6545

    Looks like someone is working on it.

    0 讨论(0)
  • 2021-02-13 07:10

    A snippet from http://boost.2283326.n4.nabble.com/No-automatic-upcasting-with-std-shared-ptr-in-function-calls-td4573165.html:

    /* make boost::python understand std::shared_ptr */
    namespace boost {
           template<typename T>
           T *get_pointer(std::shared_ptr<T> p)
           {
                   return p.get();
           }
    }
    

    Worked for me. You would define your classes like:

    class_<foo, std::shared_ptr<foo>>("Foo", ...);
    

    With this, other methods returning std::shared_ptr<foo> will Just Work.

    Some magic may be required for virtual functions/polymorphism, that should be covered in the thread I linked to.

    0 讨论(0)
提交回复
热议问题