Boost logo

Boost Users :

From: Paul (elegant_dice_at_[hidden])
Date: 2006-08-02 20:46:31


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


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