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

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
(...) 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
(...) 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

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
(...) 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
(&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
(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