Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-08-02 21:06:02


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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net