我收到错误reference_existing_object_requires_a_pointer_or_reference_return_type。
这是代码。
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>()
);
我可以知道上面的代码有什么问题吗?我收到如下编译错误。
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> ]
最佳答案
如 return_internal_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 提供了一些概念检查,并且通常类型会强调失败的概念。在这种特殊情况下,编译器错误为:
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);
}
交互式使用:
>>> import example
>>> spam = example.get_spam()
>>> assert(type(spam) is example.Spam)
关于c++ - 错误 C2027 : use of undefined type 'boost::python::detail::reference_existing_object_requires_a_pointer_or_reference_return_type<R>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182346/