Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-09-16 23:42:56


> > bind(&X::member, _1, value)(x)

On Tue, Sep 16, 2003 at 08:56:07PM -0400, Howard Hinnant wrote:
> bind(assign<T>(), bind(&X::member, _1), value)(x);
...
> The motivation for my request is that bind(f, a1, a2, ... an) currently
> has an invariant that f is a function-like object taking n parameters,
> except if f is a member pointer, then a1 is a (smart) pointer or
> reference to the object, and the function itself (if it is a function)
> takes n-1 parameters. The degenerate case of f being a member data
> pointer coincides nicely with a member function pointer taking 0
> parameters.
>
> So the motivation for my question is: Is there sufficient motivation
> to make an exception out of member data pointers, thus complicating the
> interface?

This seems to be a instance of the common interface design problem:
should the interface be small and orthogonal, or larger and
better-suited to making the common cases easy?

There are trade-offs either way. In this specific instance, I can
imagine that assigning member data in lambda/bind code is a pretty
common case, and thus in my opinion, "convenience" should win this
battle.

I don't think the added complication is too great, either. Howard's
description has already summarized the behavior of bind() using "case
analysis", and this just adds one more case. The way I see it:

   f // function pointer
   mf // member function pointer
   md // member data
   p // (smart) pointer to object

   // Here are the four cases
   bind( f, a1, ... an ) // f(a1,...an)
   bind( mf, p, a1, ... an ) // p->mf(a1,...an)
   bind( md, p ) // p->md
   bind( md, p, v ) // p->md = v // the new idea

(Note that, I think there are technically more cases that this, since
 f/mf/p can also be references, I think. This doesn't affect the
 interface in a conceptual manner, though.)

This new bind lets you do the stuff you want to do to functions (call
them or bind a subset of the arguments), and also lets you do the stuff
you want to do to data members (read values or assign new values).

MO.

-- 
-Brian McNamara (lorgon_at_[hidden])

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk