我试图找出如何支持constwchar_t*作为boostpython中公开的函数的返回类型。我的boost版本是1.52,如果有什么不同的话,我将使用python2.7。在
不知怎么的,我不能让它接受我的转换函数。我在互联网上看到过一些解决这个问题的方法,但没有一个真正有效或说明如何正确处理的。在
下面是我的一个不起作用的小例子:
#include <string>
#include <boost/python.hpp>
using namespace boost::python;
struct wchar_t_to_python_str
{
static PyObject* convert(const wchar_t* )
{
std::string s = "I'm more interested in the function signature than how to do wide char to non-wide char conversion";
return boost::python::incref(boost::python::object(s).ptr());
}
};
void init_module()
{
to_python_converter<const wchar_t*, wchar_t_to_python_str>();
}
const wchar_t* testWchar() {
return L"Hello World";
}
const char* testChar() {
return "Hello World";
}
BOOST_PYTHON_MODULE(test)
{
// This works nicely, const char* is supported
def("testChar", testChar);
// This doesn't work, fails with this error
// 'awBoost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<T>'
// def("testWchar", testWchar);
// Throwing in a return value policy fires a compile time assert make_instance_impl
// BOOST_MPL_ASSERT((mpl::or_<is_class<T>, is_union<T> >));
// It seems like it gets confused by wchar_t not being a class, but it's hard to know
def("testWchar", testWchar, return_value_policy<reference_existing_object>());
}
这个问题有几个因素:
wchar*
提供builtin conversion支持。它可能被忽略了,因为std::wstring
支持是在Boost.Python已更新为支持Python3。由于缺乏内置转换支持,内部类在编译期间需要适当的CallPolicy。提供的^{有两种方法可以在这些限制条件下工作:
wchar*
。在wchar*
的类型包装返回std::wstring
的函数。在下面是一个完整的示例,演示了这两种方法:
以及它的用法:
^{pr2}$相关问题 更多 >
编程相关推荐