c++ - 错误 C2027 : use of undefined type 'boost::python::detail::reference_existing_object_requires_a_pointer_or_reference_return_type<R>'

标签 c++ c++11 boost shared-ptr boost-python

我收到错误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/

相关文章:

c++ - 为什么这个派生类需要声明为友元?

c++ - 函数中输出容器默认值的开始

c++在存储指针元组时遇到问题

c++ - 寻找已排序的容器,其中指向元素的指针在添加/删除时不会改变

c++ - 为什么指向 int 的指针转换为 void* 而指向函数的指针转换为 bool?

c++ - 如何专门化非内联模板化成员函数? (用于不同的翻译单元)

c++ - 仅在 boost::asio basic_stream_socket::async_read_some 处理程序中使用部分数据

对 Range-v3 压缩容器进行排序 - 我可以解压吗?

c++ - 在没有公共(public)基类的情况下进行序列化

c++ - boost disable_interruption 不阻止 thread_interrupted