[C++-sig] add_property with policy?
Roman Yakovenko
roman.yakovenko at gmail.com
Mon May 22 20:07:43 CEST 2006
On 5/22/06, Neal Becker <ndbecker2 at gmail.com> wrote:
> I'd like to use add_property with a reference data member.
>
> In the following, "getA" will work fine. But, I'd like to make this work
> with attribute access syntax:
>
> python code:
> b = B()
> an_A = b.a
>
> Anyway to make this work? The add_property below won't compile:
>
> #include <boost/python/module.hpp>
> #include <boost/python/def.hpp>
> #include <boost/python/class.hpp>
> #include <boost/python/init.hpp>
> #include <boost/python/return_internal_reference.hpp>
>
> struct A {};
>
> struct B {
> A& a;
> };
>
> A& getA (B const& b) {
> return b.a;
> }
>
> using namespace boost::python;
>
> BOOST_PYTHON_MODULE (test1) {
> class_<B> ("B")
> .def ("getA", &getA, return_internal_reference<>()) << this is OK
> .add_property ("A", &getA) << this is NOT OK
> ;
> }
>
> /usr/local/src/boost.cvs/boost/python/detail/invoke.hpp:75: error: no match
> for call to '(const
> boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<A&>)
> (A&)'
You should use boost::python::make_function.
class_<B>("B")
.add_property( "a", make_function( &::getA, return_internal_reference<> ) );
This should work. Consult make_function documentation:
http://boost.org/libs/python/doc/v2/make_function.html
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list