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

Peter Dimov wrote:
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:
Yes, you need the int& here.
Here you don't need to override the return type, bind( get_ref, _1 ) will already return int&.
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