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

Jim Bosch talljimbo at gmail.com
Mon Jan 30 19:28:05 CET 2012


On 01/30/2012 12:11 PM, Michael Wild wrote:
> 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?
>

If you're up for playing with Boost.FunctionTypes, you should be able to 
write a wrapper for make_function that deduces those types for you:

namespace bp = boost::python;

template <typename A, typename T, A&(T::*method)()>
void set_helper(T & self, A const & arg) {
     (self.*method)() = arg;
}

template <class F>
bp::object make_set_helper(F f) {
     // F is the member function type; use Boost.FunctionTypes to
     // figure out A and T, then...
     return bp::make_function(&set_helper<A,T,f>);
}


I'll leave it to you to fill in the details.  I haven't tried it myself, 
but I think it would work.


Jim


More information about the Cplusplus-sig mailing list