Boost::Python raw_function 返回 void
使用 Boost::Python,包装函数的正常机制可以与返回 void
的 C++ 函数一起正常工作。不幸的是,普通机制也有局限性,特别是在它支持的功能数量方面。所以我需要使用 boost::python::raw_function 来包装我的函数,但当我的函数返回 void
时它不会编译。这是一个简单的测试用例:
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
void entry_point(boost::python::tuple args, boost::python::dict kwargs) { }
BOOST_PYTHON_MODULE(module)
{
boost::python::def("entry_point", boost::python::raw_function(&entry_point));
}
给出了错误:
/usr/local/include/boost/python/raw_function.hpp:在成员函数 'PyObject* boost::python::detail::raw_dispatcher::operator()(PyObject*, PyObject*) [with F = void (*)(boost::python::tuple, boost::python::dict)]':
/usr/local/include/boost/python/object/py_function.hpp:94:从 'PyObject* boost::python::objects::full_py_function_impl::operator()(PyObject*, PyObject*) 实例化与 Caller = boost::python::detail::raw_dispatcher, Sig = boost::mpl::vector1]'
void.cpp:8:从这里实例化
/usr/local/include/boost/python/raw_function.hpp:36:错误:void 表达式的使用无效
目前,我可以通过让我的函数返回一个虚拟值来解决这个问题,但这有点令人不满意。其他人遇到过这个问题吗?
Using Boost::Python, the normal mechanism for wrapping functions works correctly with C++ functions returning void
. Unfortunately, the normal mechanism also has limitations, specifically with regards to the function arity it supports. So I need to use boost::python::raw_function to wrap my function, but it doesn't compile when my function returns void
. Here's a simple test case:
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
void entry_point(boost::python::tuple args, boost::python::dict kwargs) { }
BOOST_PYTHON_MODULE(module)
{
boost::python::def("entry_point", boost::python::raw_function(&entry_point));
}
Which gives the error:
/usr/local/include/boost/python/raw_function.hpp: In member function ‘PyObject* boost::python::detail::raw_dispatcher::operator()(PyObject*, PyObject*) [with F = void (*)(boost::python::tuple, boost::python::dict)]’:
/usr/local/include/boost/python/object/py_function.hpp:94: instantiated from ‘PyObject* boost::python::objects::full_py_function_impl::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::raw_dispatcher, Sig = boost::mpl::vector1]’
void.cpp:8: instantiated from here
/usr/local/include/boost/python/raw_function.hpp:36: error: invalid use of void expression
For the moment, I can work around this by having my function return a dummy value, but that's somewhat unsatisfying. Have other people run into this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这就是
raw_function()
的工作方式。它期望您的函数返回一个 Python 对象。在 Python 中,最接近返回
void
的函数是返回None
的函数。我认为对于你的情况来说,这种方法是最好的(甚至不是那么难看):I think this is the way that
raw_function()
works. It expects your function to return a Python object.In Python the closest thing you will get to a function returning
void
is a function returningNone
. I think that approach would be best (and not even that ugly) in your case: