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

Michael Wild themiwi at users.sourceforge.net
Mon Jan 30 18:11:07 CET 2012


On 01/30/2012 04:58 PM, Jim Bosch wrote:
> 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

That's what I've been referring to as "auxiliary" functions. If
possible, I'd like to avoid them because I don't fancy writing hundreds
of those... I could live with calling a template. However, so far I
haven't been able to come up with one that is sufficiently easy to use...

So far I came up with the this:

template<class ArgType, class T, T&(C::*method)()>
void set_helper(T& self, ArgType& arg)
{
  (self.*method)() = arg;
}

But it is a bit cumbersome to use, as it doesn't deduce its first two
template arguments on its own. Is there any way to achieve that feat?

Michael


More information about the Cplusplus-sig mailing list