无法将 __str__ 特殊方法与 Boost::Python 接口
Cannot interface __str__ special method with Boost::Python
我正在尝试使用 Boost::Python.
link 一些 C++ 代码到 Python 对象的 __str__
特殊方法
#include <boost/python.hpp>
#include <iostream>
using namespace std;
struct POINT
{
int x,y;
POINT(int x, int y) : x(x), y(y) {}
};
ostream & operator<<(ostream & os, POINT p)
{
os << "(x:" << p.x << ", y:" << p.y << ")";
return os;
}
using namespace boost::python;
BOOST_PYTHON_MODULE(wrapper)
{
class_<POINT>("point", init<int,int>())
.def(str(self));
}
这是受official doc的启发。这会产生一个神秘的编译错误:
In file included from /usr/include/boost/python/object_core.hpp:20,
from /usr/include/boost/python/args.hpp:22,
from /usr/include/boost/python.hpp:11,
from wrapper.cpp:1:
/usr/include/boost/python/def_visitor.hpp: In instantiation of ‘static void boost::python::def_visitor_access::visit(const V&, classT&) [with V = boost::python::def_visitor<boost::python::api::object>; classT = boost::python::class_<POINT>]’:
/usr/include/boost/python/def_visitor.hpp:67:34: required from ‘void boost::python::def_visitor<DerivedVisitor>::visit(classT&) const [with classT = boost::python::class_<POINT>; DerivedVisitor = boost::python::api::object]’
/usr/include/boost/python/class.hpp:221:9: required from ‘boost::python::class_<T, X1, X2, X3>::self& boost::python::class_<T, X1, X2, X3>::def(const boost::python::def_visitor<Derived>&) [with Derived = boost::python::api::object; W = POINT; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified; boost::python::class_<T, X1, X2, X3>::self = boost::python::class_<POINT>]’
wrapper.cpp:49:20: required from here
/usr/include/boost/python/def_visitor.hpp:31:9: error: no matching function for call to ‘boost::python::api::object::visit(boost::python::class_<POINT>&) const’
v.derived_visitor().visit(c);
^
In file included from /usr/include/boost/python/args.hpp:22,
from /usr/include/boost/python.hpp:11,
from wrapper.cpp:1:
/usr/include/boost/python/object_core.hpp:160:12: note: candidate: ‘template<class ClassT, class DocStringT> void boost::python::api::object_operators<U>::visit(ClassT&, const char*, const boost::python::detail::def_helper<DocStringT>&) const [with ClassT = ClassT; DocStringT = DocStringT; U = boost::python::api::object]’
void visit(ClassT& cl, char const* name, python::detail::def_helper<DocStringT> const& helper) const
^~~~~
/usr/include/boost/python/object_core.hpp:160:12: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/python/object_core.hpp:20,
from /usr/include/boost/python/args.hpp:22,
from /usr/include/boost/python.hpp:11,
from wrapper.cpp:1:
/usr/include/boost/python/def_visitor.hpp:31:9: note: candidate expects 3 arguments, 1 provided
v.derived_visitor().visit(c);
有比我更开明的人能指出我所缺少的吗?
self
需要在self_ns
命名空间
中定义
BOOST_PYTHON_MODULE(wrapper)
{
class_<POINT>("point", init<int, int>())
.def(self_ns::str(self_ns::self));
}
不相关:更喜欢使用 const reference
而不是对象的副本到流运算符。否则,您将创建不必要的对象副本。
ostream& operator<<(ostream& os, const POINT& p)
{
os << "(x:" << p.x << ", y:" << p.y << ")";
return os;
}
我正在尝试使用 Boost::Python.
link 一些 C++ 代码到 Python 对象的__str__
特殊方法
#include <boost/python.hpp>
#include <iostream>
using namespace std;
struct POINT
{
int x,y;
POINT(int x, int y) : x(x), y(y) {}
};
ostream & operator<<(ostream & os, POINT p)
{
os << "(x:" << p.x << ", y:" << p.y << ")";
return os;
}
using namespace boost::python;
BOOST_PYTHON_MODULE(wrapper)
{
class_<POINT>("point", init<int,int>())
.def(str(self));
}
这是受official doc的启发。这会产生一个神秘的编译错误:
In file included from /usr/include/boost/python/object_core.hpp:20,
from /usr/include/boost/python/args.hpp:22,
from /usr/include/boost/python.hpp:11,
from wrapper.cpp:1:
/usr/include/boost/python/def_visitor.hpp: In instantiation of ‘static void boost::python::def_visitor_access::visit(const V&, classT&) [with V = boost::python::def_visitor<boost::python::api::object>; classT = boost::python::class_<POINT>]’:
/usr/include/boost/python/def_visitor.hpp:67:34: required from ‘void boost::python::def_visitor<DerivedVisitor>::visit(classT&) const [with classT = boost::python::class_<POINT>; DerivedVisitor = boost::python::api::object]’
/usr/include/boost/python/class.hpp:221:9: required from ‘boost::python::class_<T, X1, X2, X3>::self& boost::python::class_<T, X1, X2, X3>::def(const boost::python::def_visitor<Derived>&) [with Derived = boost::python::api::object; W = POINT; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified; boost::python::class_<T, X1, X2, X3>::self = boost::python::class_<POINT>]’
wrapper.cpp:49:20: required from here
/usr/include/boost/python/def_visitor.hpp:31:9: error: no matching function for call to ‘boost::python::api::object::visit(boost::python::class_<POINT>&) const’
v.derived_visitor().visit(c);
^
In file included from /usr/include/boost/python/args.hpp:22,
from /usr/include/boost/python.hpp:11,
from wrapper.cpp:1:
/usr/include/boost/python/object_core.hpp:160:12: note: candidate: ‘template<class ClassT, class DocStringT> void boost::python::api::object_operators<U>::visit(ClassT&, const char*, const boost::python::detail::def_helper<DocStringT>&) const [with ClassT = ClassT; DocStringT = DocStringT; U = boost::python::api::object]’
void visit(ClassT& cl, char const* name, python::detail::def_helper<DocStringT> const& helper) const
^~~~~
/usr/include/boost/python/object_core.hpp:160:12: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/python/object_core.hpp:20,
from /usr/include/boost/python/args.hpp:22,
from /usr/include/boost/python.hpp:11,
from wrapper.cpp:1:
/usr/include/boost/python/def_visitor.hpp:31:9: note: candidate expects 3 arguments, 1 provided
v.derived_visitor().visit(c);
有比我更开明的人能指出我所缺少的吗?
self
需要在self_ns
命名空间
BOOST_PYTHON_MODULE(wrapper)
{
class_<POINT>("point", init<int, int>())
.def(self_ns::str(self_ns::self));
}
不相关:更喜欢使用 const reference
而不是对象的副本到流运算符。否则,您将创建不必要的对象副本。
ostream& operator<<(ostream& os, const POINT& p)
{
os << "(x:" << p.x << ", y:" << p.y << ")";
return os;
}