在boost :: python的add_property中使用c ++ 11 lambda作为访问器函数(get_signature失败,使用lambda) [英] Using c++11 lambda as accessor function in boost::python's add_property (get_signature fails with lambda)
问题描述
我试图使用c ++ 11 lambdas作为 boost :: python中的访问器函数
' add_property
,下面是一些东西(在这个例子中lambda不是严格需要的,但是在lambda内部发生的更复杂的事情,例如验证将需要lambda):
I am trying to use c++11 lambdas as accessor functions in boost::python
's add_property
, something along the following (the lambda is not strictly needed in this example, but will be needed for more complicated things happening inside the lambda, such as validation):
#include<boost/python.hpp>
struct A{
A(): a(2){};
int a;
};
BOOST_PYTHON_MODULE(boost_python_lambda)
{
boost::python::class_<A>("A")
// .def_readonly("a",&A::a) // the classical way: works fine
.add_property("a",[](const A& a){return a.a;})
;
}
$ b < > -std = c ++ 11 (结果与g ++ 4.7相同),我得到这个错误:
However, compiling with clang++ (ver. 3.2) and -std=c++11
(the result is the same with g++ 4.7), I get this error:
/usr/include/boost/python/class.hpp:442:66: error: no matching function for call to 'get_signature'
return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0));
^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/python/class.hpp:422:22: note: in instantiation of function template specialization 'boost::python::class_<A,
boost::python::detail::not_specified, boost::python::detail::not_specified,
boost::python::detail::not_specified>::make_fn_impl<A, <lambda at boost_python_lambda.cpp:12:21> >' requested here
return this->make_fn_impl(
^
/usr/include/boost/python/class.hpp:309:40: note: in instantiation of function template specialization 'boost::python::class_<A,
boost::python::detail::not_specified, boost::python::detail::not_specified,
boost::python::detail::not_specified>::make_getter<<lambda at boost_python_lambda.cpp:12:21> >' requested here
base::add_property(name, this->make_getter(fget), docstr);
^
boost_python_lambda.cpp:12:4: note: in instantiation of function template specialization 'boost::python::class_<A,
boost::python::detail::not_specified, boost::python::detail::not_specified,
boost::python::detail::not_specified>::add_property<<lambda at boost_python_lambda.cpp:12:21> >' requested here
.add_property("a",[](const A& a){return a.a;})
^
我尝试在 std :: function< int(const A&)>(...)
中封装lambda,但是没有帮助参数扣除。任何想法?
I tried wrapping the lambda in std::function<int(const A&)>(...)
, but that did not help with the argument deduction. Any idea?
推荐答案
在这两年后,Boost.Python确实不支持包装函数对象。但你的lambda没有捕获任何东西。因此,它可以显式转换为函数指针:
Hopping in here two years later, Boost.Python indeed does not support wrapping function objects. But your lambda does not capture anything. As such, it can be explicitly convertible to a function pointer:
BOOST_PYTHON_MODULE(boost_python_lambda)
{
boost::python::class_<A>("A")
// .def_readonly("a",&A::a) // the classical way: works fine
.add_property("a", +[](const A& a){return a.a;})
↑↑↑
;
}
所有你需要的是 +
。
这篇关于在boost :: python的add_property中使用c ++ 11 lambda作为访问器函数(get_signature失败,使用lambda)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!