1
我試圖找出如何支持const wchar_t *作爲boost python中公開的函數的返回類型。我的升級版本是1.52,如果這有什麼區別,我正在使用python 2.7。將const const wchar_t *轉換爲python字符串在boost python
不知何故,我不能接受我的轉換功能。我已經在互聯網上看到了這個問題的解決方案的碎片,但沒有任何實際工作或說明如何正確執行。
這是我瑣碎的非工作例如:
#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>());
}
感謝。我設法得到第一個版本的工作(與return_value_policy),這本質上是我需要繼續前進。在Visual Studio 2012中,我沒有設法解析第二種方法,但我得到了一個令人討厭的boost編譯錯誤。 – Laserallan
如果任何人擅長boost :: mpl調試,則出錯:錯誤C3083:'function_types':'::'左側的符號必須是該行的類型typedef typename boost :: mpl :: push_front < typename boost :: function_types :: parameter_types :: type, ReturnType > :: type signature_type –
Laserallan