Reputation: 1367
I get the error reference_existing_object_requires_a_pointer_or_reference_return_type.
Here's the code.
boost::shared_ptr<CDB::Basic> GetCdbWrapper(boost::shared_ptr<A> cmd)
{
return cmd->Cdb();
}
}
virtual boost::shared_ptr<CDB::Basic> Cdb() {
boost::shared_ptr<CDB::Basic> CdbObj;
return CdbObj;
}
boost::shared_ptr<CDB::Basic> GetCdb() {
return this->Cdb();
}
class_<A, bases<Core::CommandBase>, boost::shared_ptr<A>, boost::noncopyable, >("A",
":description:\n",
boost::python::no_init
)
.def("Cdb", &A::GetCdb,
":description:\n",
return_value_policy<reference_existing_object>()
);
May I know what's wrong in above code. I get the compilation error as below.
error C2027: use of undefined type 'boost::python::detail::reference_existing_object_requires_a_pointer_or_reference_return_type<R>'
1> with
1> [
1> R=result_t
1> ]
1> c:\boost\boost_1_47_0\boost\python\detail\caller.hpp(200) : while compiling class template member function 'PyObject *boost::python::detail::caller_arity<1>::impl<F,Policies,Sig>::operator ()(PyObject *,PyObject *)'
1> with
1> [
1> F=boost::shared_ptr<CDB::Basic> (__thiscall A::* )(void),
1> Policies=boost::python::return_value_policy<boost::python::reference_existing_object>,
1> Sig=boost::mpl::vector2<boost::shared_ptr<CDB::Basic>, A &>
1> ]
Upvotes: 2
Views: 1928
Reputation: 51971
As noted in the return_internal_reference
documentation, the object returned references an existing internal object via either a pointer or reference:
return_internal_reference
[...] allow pointers and references to objects held internally [...] to be returned safely without making a copy of the referent.
Boost.Python provides some concept checks, and often the type accentuates the failed concept. In this particular case, the compiler error has:
reference_existing_object_requires_a_pointer_or_reference_return_type
This is because the GetCdb()
function returns a boost::shared_ptr<CDB::Basic>
by value, failing to meet the requirements for the return_internal_reference
call policy. To resolve this, use the default call policy of returning a copy by value. This requires that the CDB::Basic
be exposed through Boost.Python as being held by a boost::shared_ptr
. Overall, this behavior is not too different than what is often used with a shared_ptr
, where one creates copies of the shared_ptr
to maintain shared ownership.
Here is an example demonstrating this behavior:
#include <boost/python.hpp>
// Mocks...
class spam {};
boost::shared_ptr<spam> get_spam()
{
boost::shared_ptr<spam> spam_ptr(new spam());
return spam_ptr;
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<spam, boost::shared_ptr<spam>, boost::noncopyable>(
"Spam", python::no_init);
python::def("get_spam", &get_spam);
}
Interactive usage:
>>> import example
>>> spam = example.get_spam()
>>> assert(type(spam) is example.Spam)
Upvotes: 2