正在生成boost::python::范围接受。。。射程 我有一个C++类,它通过提供返回范围的函数来公开集合,使用 Boo::Reals
为了使用正在生成boost::python::范围接受。。。射程 我有一个C++类,它通过提供返回范围的函数来公开集合,使用 Boo::Reals,c++,boost-python,C++,Boost Python,为了使用boost::python将这个类导出到python,我使用函数boost::python::range,它可以接受两个参数:返回集合的开始迭代器和结束迭代器的类的成员函数 我希望避免为每个集合手动编写开始/结束对,因为我已经提供了范围。但我无法在boost::python::range上编写包装器,将其作为参数接受为返回范围的成员函数。有什么想法吗?(事实上,我有不止一个类是模板化的,所以一个模板函数以模板类的成员函数的地址作为模板参数是行不通的,我的编译器说) 如果可以用g++-4.
boost::python
将这个类导出到python,我使用函数boost::python::range
,它可以接受两个参数:返回集合的开始迭代器和结束迭代器的类的成员函数
我希望避免为每个集合手动编写开始/结束对,因为我已经提供了范围。但我无法在boost::python::range上编写包装器,将其作为参数接受为返回范围的成员函数。有什么想法吗?(事实上,我有不止一个类是模板化的,所以一个模板函数以模板类的成员函数的地址作为模板参数是行不通的,我的编译器说)
如果可以用g++-4.6编译,我将接受c++0x解决方案
编辑:按要求编辑示例代码:
假设我有这门课:
struct A
{
std::vector<int> c;
typedef boost::sub_range<std::vector<int> > c_range;
c_range getc() { return c; }
};
然后像这样暴露它们:
boost::python::class_<A>("A")
.def("getc", boost::python::range(&A::c_begin, &A::c_end));
为了避免编写
c\u begin
和c\u end
?解决方案是使用带有四个模板参数的更通用的形式arange
:将begin/end访问器创建为boost::bind
对象,然后指定range
的目标模板参数。对于常量迭代器,此代码满足我的需要:
namespace py = boost::python
template <class T, class Return>
struct range_accessor {
typedef Return (T::*RA ) () const;
static typename Return::const_iterator
begin(RA ra, const T& t) {
return (t.*ra)().begin();
}
static typename Return::const_iterator
end(RA ra, const T& t) {
return (t.*ra)().end();
}
static py::object
pyrange(RA ra) {
auto b = boost::bind(&range_accessor::begin, ra, _1);
auto e = boost::bind(&range_accessor::end, ra, _1);
return py::range<
boost::python::objects::default_iterator_call_policies,
T> // the "Target" parameter, which can
// not be deduced from a bind object
(b,e);
}
};
template <class T, class Return>
py::object pyrange(Return (T::*ra ) () const) {
return range_accessor<T, Return>::pyrange(ra);
}
namespace py=boost::python
模板
结构范围存取器{
typedef返回(T::*RA)()常量;
静态类型名返回::常量迭代器
开始(RA、const T&T){
返回(t.*ra)().begin();
}
静态类型名返回::常量迭代器
完(RA、const T&T){
返回(t.*ra)(.end();
}
静态py::对象
皮兰热{
自动b=boost::bind(&range\u访问器::begin,ra,\u 1);
自动e=boost::bind(&range\u访问器::end,ra,\u 1);
返回py::范围<
boost::python::objects::默认迭代器\调用\策略,
T> //参数“Target”,它可以
//不能从绑定对象推断
(b,e);
}
};
模板
py::object pyrange(返回(T::*ra)()常量){
返回范围\存取器::吡喃(ra);
}
编辑:通过在函数定义中使用局部结构,一种更紧凑的解决方案:
template <class T, class Return>
py::object
pyrange(Return (T::*ra) () const) {
typedef Return (T::*RA ) () const;
struct accessor {
static typename Return::const_iterator
begin(RA ra, const T& t) {
return (t.*ra)().begin();
}
static typename Return::const_iterator
end(RA ra, const T& t) {
return (t.*ra)().end();
}
};
return py::range<boost::python::objects::default_iterator_call_policies, T>
(boost::bind(&accessor::begin, ra, _1),
boost::bind(&accessor::end, ra, _1));
}
模板
py::对象
pyrange(返回(T::*ra)()常量){
typedef返回(T::*RA)()常量;
结构存取器{
静态类型名返回::常量迭代器
开始(RA、const T&T){
返回(t.*ra)().begin();
}
静态类型名返回::常量迭代器
完(RA、const T&T){
返回(t.*ra)(.end();
}
};
返回py::范围
(boost::bind(&accessor::begin,ra,_1),
boost::bind(&accessor::end,ra,_1));
}
你能以代码的形式展示一个例子吗?啊,我一直在寻找这样的代码例子。。问这个问题有点晚了,但是您能不能用.def
方法向Python展示一下您最终是如何将它公开的?我刚才问了一个非常类似的问题
namespace py = boost::python
template <class T, class Return>
struct range_accessor {
typedef Return (T::*RA ) () const;
static typename Return::const_iterator
begin(RA ra, const T& t) {
return (t.*ra)().begin();
}
static typename Return::const_iterator
end(RA ra, const T& t) {
return (t.*ra)().end();
}
static py::object
pyrange(RA ra) {
auto b = boost::bind(&range_accessor::begin, ra, _1);
auto e = boost::bind(&range_accessor::end, ra, _1);
return py::range<
boost::python::objects::default_iterator_call_policies,
T> // the "Target" parameter, which can
// not be deduced from a bind object
(b,e);
}
};
template <class T, class Return>
py::object pyrange(Return (T::*ra ) () const) {
return range_accessor<T, Return>::pyrange(ra);
}
template <class T, class Return>
py::object
pyrange(Return (T::*ra) () const) {
typedef Return (T::*RA ) () const;
struct accessor {
static typename Return::const_iterator
begin(RA ra, const T& t) {
return (t.*ra)().begin();
}
static typename Return::const_iterator
end(RA ra, const T& t) {
return (t.*ra)().end();
}
};
return py::range<boost::python::objects::default_iterator_call_policies, T>
(boost::bind(&accessor::begin, ra, _1),
boost::bind(&accessor::end, ra, _1));
}