boost::bind for returning a reference?

Hi all, I understand why boost::bind() must return a const-reference or a value, but what I don't understand is, why must I do boost::bind<R&>(...) if I want it to return by reference? Why must I (the user) deduce the return type myself? Can't we have a boost::bind_return_reference(...) function that is the equivalent of boost::bind<R&>(...), so I don't have to do all the hard result_of type deduction manually? cheers Paul

Paul wrote:
Hi all,
I understand why boost::bind() must return a const-reference or a value, but what I don't understand is, why must I do boost::bind<R&>(...) if I want it to return by reference?
Why must I (the user) deduce the return type myself?
Can you expand your ... a bit in order to help me understand your question better?

Peter Dimov wrote:
Paul wrote:
I understand why boost::bind() must return a const-reference or a value, but what I don't understand is, why must I do boost::bind<R&>(...) if I want it to return by reference?
Why must I (the user) deduce the return type myself?
Can you expand your ... a bit in order to help me understand your question better?
I'll try and explain without getting too long-winded... struct AClass { typedef int var_type; int var; }; I want a function that returns a reference to the int, so i can assign stuff to it... bind<int&>(&AClass::var,_1) = whatever; I have a function that returns a reference to a member: int & get_ref( AClass & a ) { return a.ivar; } now i want to bind to it: bind<int&>(get_ref,_1) = whatever; I have a functor that returns a reference to some member template <class TheClass> struct GetRef { typename TheClass::var_type & operator()( TheClass & c ) const { return c.ivar; } }; now to bind it to AClass's ivar... bind<int&>( GetRef<AClass>(), _1 ) = whatever; but if i had a 'BClass' that had a double instead... struct BClass { typedef double var_type; double var; }; bind<double&>( GetRef<BClass>(), _1 ) = whatever; and if i wanted to do it generically, now i need to do a lot more work... template <class TheClass> void doit( TheClass & c ) { bind< typename TheClass::var_type & >(&TheClass::var, _1) = whatever; } and so on. this can get a lot more complicated. my question is, bind() already knows what the type is. The problem is that it always returns a const& or by value. Can't we have a way of forcing it to return a mutable reference when necessary? I'd like to be able to just do: template <class TheClass> void doit( TheClass & c ) { bind_ref(&TheClass::var, _1) = whatever; } and not have to worry about figuring out the return type myself. cheers Paul

Paul wrote:
Peter Dimov wrote:
Paul wrote:
I understand why boost::bind() must return a const-reference or a value, but what I don't understand is, why must I do boost::bind<R&>(...) if I want it to return by reference?
Why must I (the user) deduce the return type myself?
Can you expand your ... a bit in order to help me understand your question better?
I'll try and explain without getting too long-winded...
struct AClass { typedef int var_type; int var; };
I want a function that returns a reference to the int, so i can assign stuff to it... bind<int&>(&AClass::var,_1) = whatever;
Yes, you need the int& here.
I have a function that returns a reference to a member:
int & get_ref( AClass & a ) { return a.ivar; }
now i want to bind to it: bind<int&>(get_ref,_1) = whatever;
Here you don't need to override the return type, bind( get_ref, _1 ) will already return int&.
I have a functor that returns a reference to some member
template <class TheClass> struct GetRef { typename TheClass::var_type & operator()( TheClass & c ) const { return c.ivar; } };
You need to add a result_type typedef to the class: template <class TheClass> struct GetRef { typedef typename TheClass::var_type & result_type; result_type operator()( TheClass & c ) const { return c.ivar; } }; and bind( GetRef<AClass>(), _1 ) will return it.
participants (2)
-
Paul
-
Peter Dimov