[C++-sig] Boost.Python property definition question

Jim Bosch talljimbo at gmail.com
Mon Jan 30 16:58:07 CET 2012


On 01/30/2012 09:32 AM, Michael Wild wrote:
> Hi all
>
> I can't seem to figure out how to wrap this in Boost.Python without the
> need for auxiliary functions:
>

<snip>

>    string&  s() { return m_s; }

The second argument to add_property should be an actual setter, not a 
non-const-reference getter.  In this case, you can write another free 
function that delegates to the non-const-getter, then wrap that:

void set_s(A & a, std::string const & s) { a.s() = s; }

...

.add_property("s",
     make_function(
        (string const&(A::*)()const)&A::s,
        return_value_policy<copy_const_reference>()),
     make_function(&set_set)
)



HTH

Jim Bosch


More information about the Cplusplus-sig mailing list