2

I was wondering if any one here might be able to help me out with the following problem I'm having.

I seem unable to create a boost::python::object from a c++ class I've bound to python that is noncopyable. Here is a simplified example..

#include <boost/python.hpp>

class A
{
public:
    static A*
    create() {return new A;}

protected:
    A(){}
};

void
doSomething(const A& a)
{
    boost::python::object obj(a);
}

BOOST_PYTHON_MODULE(test)
{
    boost::python::class_<A, boost::noncopyable>("A", boost::python::no_init)
    .def("__init__", boost::python::make_constructor(&A::create));

    boost::python::def("doSomething", &doSomething);
}

Then at runtime in python

import test
a = test.A()
test.doSomething(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: A

I realise that the boost::noncopyable parameter prevents a to_python converter for A being registered. Does anyone know how I might be able to create a boost::python::object from an A instance ?

thanks in advance!

1 Answer 1

2

Use this

boost::python::object obj(**boost::cref(a)**);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.